Calling Activity ( calling from one page to another)
Displaying Selected Book
Fetching Result from a called activity - Android Tutorial for Beginners – (Part 5)
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 methodstartActivity().However, when we want a result to be returned by the child activity, we need to call it by the methodstartActivityForResult(). When the child activity finishes with the job, it should set the data in an intent and call the methodsetResult(resultcode, intent)to return the data through the intent.
The parent activity should have overridden the methodonActivityResult(…)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 callfinish()aftersetResult(..)in order to give back the handle to the parent activity.
In summary, here are the methods to implement in the parent activity:
The child Activity should complete the work as usual and finally call:
The calling Activity is providing 2 buttons to view books and pens. On selecting one of them, eitherBooksActivityorPensActivityis 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:
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 theBooksActivity. 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 =newIntent();
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 thesetResult(…)method call. The result code is set toRESULT_OKsince the job has been successfully done. After that thefinish()method is called to give the control back to the parent activity.
In the parent, the method that gets the control isonActivityResult(…)
String name = data.getStringExtra("SelectedBook");
Toast.makeText(this,"You have chosen the book: "+" "+ name, Toast.LENGTH_LONG).show();
Here you notice that theBOOK_SELECTconstant 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.