Pages

Saturday, March 19, 2016

Android Intents Part 2 Passing data and returning results between activities

In a previous post we said that intents describe that an action to be performed. we saw 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 activity were going to see 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 how 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);
startActivity(intent);
}
});

and the same for each button in the other activities.

we create the intent with a constructor that takes two parameters:
  1. Context: a reference to the current activity.
  2. 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 dont have a feedback on the whether the launched activity finished its 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 secondactivity 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:
  1. The intent to start.
  2. Request code: which is an integer identifier that is used as a corelation id to identify which sub activity has finished its 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:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
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 buttons 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);
finish();
}

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.

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

String extraData=data.getStringExtra("ComingFrom"));
}

so this was how intents can be used to pass data and return results between several activities within the same applications.
H9GYCEXU8DZF

Related Posts by Categories

0 comments:

Post a Comment