7

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)?

ashishduh
  • 6,629
  • 3
  • 30
  • 35
  • do you want like this => start activity A => Activity B => Activity C, here you press UP button, come back to Activity B.. – Chintan Rathod Jun 21 '13 at 14:38
  • Yes, I've edited my question to describe the situation. – ashishduh Jun 21 '13 at 14:40
  • 1
    Could you clarify the problem you're actually having? From the question it is unclear – jkau Jun 21 '13 at 14:41
  • Yes. You can just call "finish" if you have started activity by `startActivityOnResult`. When user press UP button, set result and finish. – Chintan Rathod Jun 21 '13 at 14:41
  • @jkau I guess my main problem is I don't understand the purpose of implementing "Up" as the auto-generated code stub does. It seems to me that lots of activites would have some initialization that may require extras that the calling activity wouldn't have access to. I'm just wondering if I'm missing something. – ashishduh Jun 21 '13 at 14:48

2 Answers2

8

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.

Community
  • 1
  • 1
yonojoy
  • 5,486
  • 1
  • 31
  • 60
  • Maybe a typo, but I just had the same situation and I had to put the `singleTop` launch mode on activity B (the middle one that has both child and parent). – Sarah Vessels Feb 02 '14 at 16:11
  • @SarahVessels You are absolutely right, I corrected the answer. The launch mode for activity B has to be declared `"singleTop"`. `navigateUpFromSameTask` navigates from C to B. But with default launch mode this means _"a new instance of the class [B] is created"_. If launch mode of B is declared `"singleTop"` then that means it will return to the existing instance as long as it is on the top of the back stack. If the target activity is not on top of the back stack (e.g. navigate to A) one can use [this solution](http://stackoverflow.com/a/20421306/2306907). – yonojoy Feb 02 '14 at 17:44
0

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.

Community
  • 1
  • 1
jkau
  • 465
  • 4
  • 12