Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art
HomeF.A.Q.
Community
Writing Recipes
Login
Retrieving data from a Sub-Activity back to your Main Activity
Contributed by Ulysses Levy 2010-11-15 11:26:41 (updated 2011-09-24 04:21:13)
In Published Edition? Yes
0
Votes
Problem

Your main activity needs to retrieve data from a sub activity.

Solution

Use startActivityForResult(), and onActivityResult() in the main activity, and setResult() in the sub-activity.

Discussion

In this example we return a string from a Sub-Activity (MySubActivity) back to the Main Activity (MyMainActivity).

The first step is to "push" data from MyMainActivity via the Intent mechanism.

public class MyMainActivity extends Activity
{
    //..for logging..
    private static final String TAG = "MainActivity";
        
    //..The request code is supposed to be unique?..
    public static final int MY_REQUEST_CODE = 123;
    
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        ...
    }
    
    private void pushFxn()
    {
        Intent intent = 
            new Intent( 
                this, 
                MySubActivity.class );
                
        startActivityForResult( intent, MY_REQUEST_CODE );
    }
    
    protected void onActivityResult(
            int requestCode, 
            int resultCode,
            Intent pData) 
    {
         if ( requestCode == MY_REQUEST_CODE ) 
         {
             if (resultCode == Activity.RESULT_OK ) 
             {
                 final String zData = pData.getExtras().getString( MySubActivity.EXTRA_STRING_NAME );
                 
                 //..do something with our retrieved value..
                 
                 Log.v( TAG, "Retrieved Value zData is "+zData );
                 //..logcats "Retrieved Value zData is returnValueAsString" 
                 
             }
         }
         
     }
}

Notes:

  • The Main Activity's onActivityResult() gets called after MySubActivity.finish().
  • The retrieved value is technically an Intent, and so we could use it for more complex data (such as a uri to a google contact or something). However, in the above example code, we are only interested in a string value via Intent.getExtras().
  • The requestCode (MY_REQUEST_CODE) is supposed to be unique, and might be useful later. ie. Activity.finishActivity( MY_REQUEST_CODE )

The second major step is to "pull" data back from MySubActivity to MyMainActivity.

public class MySubActivity extends Activity
{
    public static final String EXTRA_STRING_NAME = "extraStringName";
    
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        ...
    }        
    
        
    private void pullFxn()
    {
        Intent iData = new Intent();
        iData.putExtra( 
            EXTRA_STRING_NAME, 
            "returnValueAsString" );
        
        setResult( 
            android.app.Activity.RESULT_OK,
            iData );
        
        //..returns us to the parent "MyMainActivity"..
        finish();
    }
}

Code Notes:

  • Once again, Intents are used as data (ie. "iData").
  • setResult() requires a result code such as RESULT_OK.
  • finish() essentially pushes the result from setResult().

Other

  • Technically, the data from MySubActivity doesn't get "pull"-ed until we're back on the other side with MyMainActivity. So arguably it is more similar to a 2nd "push".
  • We don't have to use a public static final String variable for our "extra" field name, but I thought it was good style.

Use Case (informal)

In my app, I have a ListActivity with a ContextMenu (user long presses a selection to do something), and I wanted to let the Main-Activity know which row the user had selected for the ContextMenu action ( atm, my app only has one action ). I ended up using intent extras to pass the selected row's index as a string back to the parent activity; From there I could just convert the index back to an int and use it to identify the user row selection via ArrayList.get( index ). This worked for me, however I am sure there is another/better way.

Comments (0)
Leave a comment
Edit History (13)
There are no (moderator-approved) comments on this recipe yet.