0

I'd like to implement the following behavior for the up-button:

  • If an instance of the parent activity is on the back stack, return to the top-most instance of that parent activity.
  • If there is no instance of the parent activity on the back stack, create a new instance.

Actually, when I first read about the difference between up and back, this behaviors was what I expected as default. However, I have seen now, that NavUtils.navigateUpFromSameTask(this); always creates a new instance of the parent activity, ignoring the fact, that there is one readily available on the back stack.

Thanks for any advice.

Chris
  • 3,192
  • 4
  • 30
  • 43

2 Answers2

1

Try to specify launch mode for your activity in Manifest android:launchMode="singleInstance" or android:launchMode="singleTop" ,look at this question for more info Android launchmode="singleTask" does not work as expected

Community
  • 1
  • 1
Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
0

If an instance of the parent activity is on the back stack, return to the top-most instance of that parent activity.

If this is meant to clear the back stack down to this top-most instance, I recently solved this issue customizing the up-navigation using the activity's onOptionsItemSelected() and adding the FLAG_ACTIVITY_CLEAR_TOP-flag (link) to the Intent - while only switching the launch mode did not end up in the back stack I was expecting:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    Intent parentActivityIntent = NavUtils.getParentActivityIntent(this);
    parentActivityIntent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(parentActivityIntent);
}

Also have a look at http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp.

speisz
  • 3
  • 1