The next logical step in learning the Android development is to look at how can you call or invoke one activity from another and get back data from the called activity back to the calling activity. For simplicity sake, let us name the first calling activity as parent activity and the invoked activity as the child activity.
For simplicity sake, I 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.
The parent activity should have overridden the method onActivityResult(…) in order to be able to get the data and act upon it.
NOTE: for successful execution of this sequence of events, the child activity should call finish() after setResult(..) in order to give back the handle to the parent activity.
In summary, here are the methods to implement in the parent activity:
- 1. startActivtyForResult(..)
- 2. onActivityResult(…)
The child Activity should complete the work as usual and finally call:
- 1. setResult(…)
- 2. finish()
For Downloading Entire project Click Here :
The calling Activity is providing 2 buttons to view books and pens. On selecting one of them, either BooksActivityor PensActivity is called, which displays a list (using ListView) of the selected type of objects. The user can select one and the selected object is returned to the parent for display. (Note: this could be extended into a shopping cart example. I have kept it simple for the tutorial’s sake)
Since we are expecting to get back the selected object, the calling Activity’s code is like this:
Intent bookIntent = new Intent(); bookIntent.setClass(CallingActivity.this,BooksActivity.class);
startActivityForResult(bookIntent,BOOK_SELECT);
where BOOK_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 theCallingActivity. This is how it is done:
Object o = this.getListAdapter().getItem(position);
String book = o.toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("SelectedBook",book);
setResult(RESULT_OK,returnIntent);
finish();
The first 2 lines show how to get the selected book 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 toRESULT_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 BOOK_SELECT:
if (resultCode == RESULT_OK) {
String name = data.getStringExtra("SelectedBook");
Toast.makeText(this, "You have chosen the book: " + " " + name, Toast.LENGTH_LONG).show();
break;
}
……….
}
}
Here you notice that the BOOK_SELECT constant is used to act upon the result. If the result code isRESULT_OK, we take the book selected from the “extra” of the intent that is returned from the child activity.data.getStringExtra("SelectedBook") is called to and the name returned is displayed through a Toast.
No comments:
Post a Comment