ashishduh
ashishduh

Reputation: 6699

Navigating Up to an activity that expects extras

I have Activities A, B, and C in my app and they flow in that order. If I implement the Up button in C, I want it to go back to B. The code stub that Eclipse generated is this:

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId())
    {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

However, B expects extras to be passed from A in its onCreate method. The relationship between B and C is that C allows the user to set some settings that affect how B is displayed. I'm currently handling saving the changes in C in onPause(). Should I just finish() C when user presses Up instead of calling navigateUpFromSameTask(this)?

Upvotes: 7

Views: 1470

Answers (2)

yonojoy
yonojoy

Reputation: 5566

If you're going to be returning from C back to B your activity will be created again, if you use standard launch mode. onSaveInstanceState will not (reliably) work.

Declare the launch mode of your activity B as:

android:launchMode="singleTop"

in your AndroidManifest.xml, if you want to return to your activity. See docu and an explanation here.

Upvotes: 8

jkau
jkau

Reputation: 465

I believe you're having a similar problem to this? Essentially, if you're going to be returning from C back to B, there's a possibility it's going to need to call onCreate() again. This means that the extras you obtain from the intent are going to be gone, so you have to store them with something like onSaveInstanceState.

Upvotes: 0

Related Questions