Friday, August 24, 2012

Passing Data Between Two Activities

In this post we will discuss about how to pass data between two activities. This is very important feature in perspective of the any application that it always needed to pass data between two activities. Here we will use two activities two pass the data first calling activity is called as the parent activity and other is invoked activity child activity.

For simplicity sake, We use an explicit intent for invoking the child activity. For simple invocation without expecting any data back, we use the method startActivity(). However, when we want a result to be returned by the child activity, we need to call it by the method startActivityForResult(). When the child activity finishes with the job, it should set the data in an intent and call the method setResult(resultcode, intent) to return the data through the intent.

When the parent activity expecting result from the invoked activity. The parent activity should have overridden the method onActivityResult(…) in order to be able to get the data and act upon it. 

After the setting up the set activity setResult(...) please call finish() function to return to the parrent activity.

Here we need two methods to impliment on the parrent activity.

  1. startActivtyForResult(..)
  2. onActivityResult(..) 
The child activity must execute its line of codes after then it have to call the following two functions.
  1. setResult(..)
  2. finish()
You can download the working example from here

Here in this example we can see easily our calling activity having two buttons to view He or She. Either selecting one of them HeAcitivty or SheActivity which display a list using listview of selected type of objects. The user can select any of the object then the object will return it to the parent activity with data.


Since we are expecting to get back the selected object, the calling Activity’s code is like this:
     Intent HeIntent = new Intent();                heIntent.setClass(Home.this,HeActivity.class);
startActivityForResult(heIntent,HE_SELECT);
where HE_SELECT is just a constant to help us identify from which child activity the is result obtained, when there is more than 1 child activity, as in this case.

At this point the control is handed over to the BooksActivity. This displays the list of books and the user can scroll through and select a book. When the user selects a book, the selected book needs to be passed back to the CallingActivity. This is how it is done:
      Object o = this.getListAdapter().getItem(position);
      String book = o.toString();
      Intent returnIntent = new Intent();
      returnIntent.putExtra("Selected He",book);
      setResult(RESULT_OK,returnIntent);       
      finish();

The first 2 lines show how to get the selected he from the ListView. Then, you create a new intent object, set the selected book as an extra and pass it back through the setResult(…) method call. The result code is set to RESULT_OK since the job has been successfully done.  After that the finish() method is called to give the control back to the parent activity.

In the parent, the method that gets the control is onActivityResult(…)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
      {
      switch(requestCode) {
      case HE_SELECT:
            if (resultCode == RESULT_OK) {
                String name = data.getStringExtra("Selected He");
                Toast.makeText(this, "You have chosen the He: " + " " + name, Toast.LENGTH_LONG).show();
                break;
            }
      ……….
      }
      }  

Here you notice that the HE_SELECT constant is used to act upon the result. If the result code is RESULT_OK, we take the book selected from the “extra” of the intent that is returned from the child activity. data.getStringExtra("SelectedHe") is called to and the name returned is displayed through a Toast.

1 comment:

  1. It was a very useful post...
    thank you

    ReplyDelete