Being able to start one activity from another is all well and good, but we will often need to know how the called activity has fared in its task or even which activity has been called. The startActivityForResult()
method provides the most straightforward way to do this.
Returning a result from an activity is not that different from calling one the way we did in the previous recipe. Start up a new Android project in Eclipse and call it GettingResults
.
In this recipe we will need to create a new activity class, provide it with an onCreate()
method, then edit our default class and include our new activity in the manifest file:
Create a new class called
MyNewActivity
in the same package as theGettingResults
class and give it the Superclassandroid.app.Activity
.Extend the class as an activity, provide it with an
onCreate()
method, and fill it out as given next.Pressing Ctrl + Space once you have typed as far as
public void onCrea
will prompt Eclipse to complete most of this method for you:public class MyNewActivity extends Activity { @Override public void onCreate(Bundle state) { super.onCreate(state); setResult(42); finish(); } }
Press Ctrl + Shift + O. This will import the following libraries:
import android.app.Activity; import android.os.Bundle;
Open the
GettingResults
class and edit it to look like this:public class GettingResults extends Activity { @Override public void onCreate(Bundle state) { super.onCreate(state); setContentView(R.layout.main); Intent i = new Intent(this, MyNewActivity.class); startActivityForResult(i, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Toast.makeText(this, Integer.toString(resultCode), Toast.LENGTH_LONG).show(); } }
Import any library the class needs with Ctrl + Shift + O.
Open the manifest file and include a new
<activity>
element underneath the one that is already there. Include the following attributes:<activity android:name=".MyNewActivity" android:label="my new activity"> </activity>
Run the application on a device or an emulator. A small pop-up appears in the initial activity that has been passed from the called one:
Here, the called activity used setResult()
to return a result code back to the calling activity. We used an arbitrary value in our example but a result code can be used to represent an outcome such as the index of a selected item.
The corresponding member in the calling activity is the onActivityResults()
method. Besides the result code that we just sent from the called activity, the method receives a request code. This is simply the integer value that was passed with the startActivityForResult()
call which takes the form:
startActivityForResult(Intent intent, int requestCode);
We used 0 as our request code because we knew where it came from—but this value can be used to identify where the request originated in less trivial applications with several activities.
Note
If startActivityForResult()
is called with a negative request code it will act exactly as if it were a call to startActivity()
—that is, it will not return a result.
Activity results are always handled by the calling activity's onActivityResults()
method, which makes use of the
request code as well as the result code.
We made use of the Toast object which is a neat little pop-up view that can be used to unobtrusively inform the user of some event or the other. It also functions as a handy little tool for on-the-fly debugging as it doesn't need setting up or screen estate.
In the previous example, we only returned an integer with setResult()
but there is an alternative format.
To learn more about creating new activity classes refer to the previous recipe, Switching between activities.
For more information on Toasts see the recipe Making a Toast in Chapter 7, Notifying the user.