Returning Results from Activities

An Activity started via startActivity is independent of its parent and will not provide any feedback when it closes.

Alternatively, you can start an Activity as a sub-Activity that's inherently connected to its parent. A sub-Activity triggers an event handler within its parent Activity when it closes. Sub-Activities are perfect for situations in which one Activity is providing data input (such as a user's selecting an item from a list) for another.

Sub-Activities are really just Activities opened in a different way. As such they must be registered in the application manifest —in fact any manifest-registered Activity can be opened as a sub-Activity including system or third-party application Activities.

Launching Sub-Activities

The startActivityForResult method works much like startActivity, but with one important difference. As well as the explicit or implicit Intent used to determine which Activity to launch, you also pass in a request code. This value will later be used to uniquely identify the sub-Activity that has returned a result.

The skeleton code for launching a sub-Activity is shown in Listing 5-3.

LISTING 5-3: Starting an Activity for a result Available for download on private static final int SHOW_SUBACTIVITY = 1;

Intent intent = new Intent(this, MyOtherActivity.class); startActivityForResult(intent, SHOW_SUBACTIVITY);

Like regular Activities, sub-Activities can be started implicitly or explicitly. Listing 5-4 uses an implicit Intent to launch a new sub-Activity to pick a contact.

LISTING 5-4: Implicitly starting an Activity for a result

Available for download on private static final int PICK_CONTACT_SUBACTIVITY = 2;

Uri uri = Uri.parse("content://contacts/people"); Intent intent = new Intent(Intent.ACTION_PICK, uri); startActivityForResult(intent, PICK_CONTACT_SUBACTIVITY);

Returning Results

When your sub-Activity is ready to return, call setResult before finish to return a result to the calling Activity.

The setResult method takes two parameters: the result code and the result itself, represented as an Intent.

The result code is the''result'' of running the sub-Activity—generally eiAhtiivity.RESULT_OK or Activity.RESULT_CANCELED. In some circumstances you'll want to use your own response codes to handle application specific choices; setResult supports any integer value.

The Intent returned as a result often includes a URI to a piece of content (such as the selected contact, phone number, or media file) and a collection of extras used to return additional information.

Listing 5-5 is taken from a sub-Activity'onCreate method, and shows how an OK and Cancel button might return different results to the calling Activity.

LISTING 5-5: Creating new Shared Preferences

Available for download on Button okButton = (Button) findViewById(; okButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

Uri data = Uri.parse("content://horses/" + selected_horse_id);

Intent result = new Intent(null, data); result.putExtra(IS_INPUT_CORRECT, inputCorrect); result.putExtra(SELECTED_PISTOL, selectedPistol);


LISTING 5-5 (continued)

setResult(RESULT_OK, result); finish();

Button cancelButton = (Button) findViewById(; cancelButton.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { setResult(RESULT_CANCELED, null); finish();

If the Activity is closed by the user pressing the hardware back key, or finish is called without a prior call to setResult, the result code will be set to result_canceled and the result Intent set to null.

Handling Sub-Activity Results

When a sub-Activity closes, the onActivityResult event handler is fired within the calling Activity. Override this method to handle the results returned by sub-Activities.

The onActivityResult handler receives a number of parameters:

> Request code The request code that was used to launch the returning sub-Activity.

> Result code The result code set by the sub-Activity to indicate its result. It can be any integer value, but typically will be either Activity.RESULT_OK or Activity.RESULT_CANCELED.

If the sub-Activity closes abnormally, or doesn't specify a result code before it closes, the result code is Activity.RESULT_CANCELED.

> Data An Intent used to package returned data. Depending on the purpose of the sub-Activity, it may include a URI that represents a selected piece of content. Alternatively, or additionally, the sub-Activity can return extra information as primitive values using the Intent extras Bundle.

The skeleton code for implementing the onActivityResult event handler within an Activity is shown in Listing 5-6.

LISTING 5-6: Implementing an On Activity Result Handler

Available for download on private static final int SHOW_SUB_ACTIVITY_ONE = 1; private static final int SHOW_SUB_ACTIVITY_TWO = 2;

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

super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {

if (resultCode == Activity.RESULT_OK) { Uri horse = data.getData();

boolean inputCorrect = data.getBooleanExtra(IS_INPUT_CORRECT, false); String selectedPistol = data.getStringExtra(SELECTED_PISTOL);


if (resultCode == Activity.RESULT_OK) { // TODO: Handle OK click.


Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment