Android pass variables between activities


Android App Development: Using intents to pass data and return results between activities

In my previous tutorial I explained how intents describe an action to be performed. We learned how to launch phone activities like phone dialer with intents, and how we passed data (The phone number to dial) to the phone activity.

In this post I’m going to see show you how to use intents to launch several activities within the same application and how to pass data between them.

We will make an application with three activities, each activity has a button that when pressed navigates to the next activity in a round-robin fashion.
this is what each activity looks like:

Each button will navigate to the next activity like this:

btn1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(IntentsDemo2.this,Activity2.class);

and the same for each button in the other activities.

We created the intent with a constructor that takes two parameters:

Context: a reference to the current activity.
Class: the type of the activity to be launched by the intent.

Passing Results between Intents:

By default when you launch an activity using an intent you don’t have a feedback on the whether the launched activity finished it’s work or not.

We can launch an activity as a sub activity of a parent activity. This means that when the sub activity closes, it triggers an event handler in the parent.

Consider the previous example, we want each activity to display the name of the activity that was displayed before it. So we launch the intent normally but we use the Intent.putExtra(String Name,String Value) method to pass any extra data needed.

In the first activity we launch the second activity like this:

Intent intent=new Intent(IntentsDemo2.this,Activity2.class);
intent.putExtra(“ComingFrom”, “Acticity 1″);
final int result=1;
startActivityForResult(intent, result);

We did not use the startActivity method as we did before, instead we used startActivityForResult(Intent intent, int requestCode)
thich takes two parameters:

The intent to start.
Request code: which is an integer identifier that is used as a corelation id to identify which sub activity has finished it’s work (will explain later) .

So the above code launches a sub activity and passes it an extra peice of information via Intent.putExtra method.
Receiving Extra data in sub activity:

Now the sub activity has been started with an intent from the parent with some extra data. to retreive this data in the sub activity we use the getIntent() method.

The getIntent() method returns a reference t the intent tha started the sub activity.

So in the sub activity we can call the method like this:

public void onCreate(Bundle savedInstanceState) {
Intent sender=getIntent();
String extraData=sender.getExtras().getString(“ComingFrom”);

This retreives the extra data added to the intent in the parent activity.
Handling sub activity results:

Now suppose that the parent activity wants to know what was the result returned from the sub activity or wants to get some data from the sub activity, this is handled by overriding the onActivityResult method in the parent activity.

Now suppose that the sub activity wants to pass a string containg the word “Hello” to the parent activity.

In the sub activity there is a button that when pressed returns to the parent activity. The button’s click event handler can be like this:

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra(“ComingFrom”, “Hello”);
setResult(RESULT_OK, intent);

Here we used the intent.putExtra method to pass the extra data.

we also used the setResult(int result,Intent intent) method to set a result code to be sent to the caller parent activity.
The result code often has two predefined values Result_OK or Result_CANCELED. you can define any result value you want.

Also we called finish() method that closes the activity and returrns to the caller activity.

When the finish() method is invoked in the sub activity, the onActivityResult callback method is invoked in the caller activity.

So when overriding the onActivityResult method in the caller activity we can get the data passed from the sub activity.

public void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode, resultCode, data);

String extraData=data.getStringExtra(“ComingFrom”));

This was how intents can be used to pass data and return results between several activities within the same applications. Stay tuned for more Android tutorials next week.
Similar articles:

Android App Development: Intents and Intent filters
Android App development: Tabbed Activities
Android App Development: Menus part 1: Options menu
iOS Development Tutorial Series: Calculator Part 2

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>