Sunday, 15 July 2012

Moving from one activity to another in android

     

     Moving from one activity to another in android 



Intents are used to move one activity to another in android.following are the some of the methods to invoke the intents.

  1. startActivity(intent)
  2. sendBroadcast(intent)
  3. startService(intent)
  4. bindService(intent)
  in the startActivity of intent is used to start the another activity and the next one sendBroadcast(intent) is for sending to any broadcast receiver and the other two is used for interact with the background service.  

the basic syntax for the intent is given below


Intent i=new Intent(this,abc.class);
startActivity(i);


following is a simple program for intent


 Intentdemo1.java


import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


public class Intentdemo1Activity extends Activity

{

  Button b1;

 EditText e1;

      @Override

    public void onCreate(Bundle savedInstanceState) 

{

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

           b1=(Button)findViewById(R.id.button1);

    }

    public void sendmsg(View v)

    {

    if (v==b1)

    {

    EditText e1=(EditText)findViewById(R.id.edit1);

    Intent i=new Intent(this,intentdemo2.class);

    startActivity(i);

         Toast.makeText(this, "haiiiiii",Toast.LENGTH_LONG).show();

    }

    }

    }


intentdemo2.java


package a.b;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.Button;

import android.widget.TextView;


public class msgclass extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

     setContentview(R.layout.main);

           }

}





No comments:

Post a Comment