1

I am implementing the Up button in the ActionBar using this method posted here:

ActionBar Up button and Navigation pattern

It works ok except in one scenario: If Activity A creates Activity B, and then I press Up it will navigate to A no problem.

However, when I get to Activity B, and then I switch to another App, then switch back to my App, and now I press the Up button, it will navigate me to the home screen instead of Activity A.

When I debug I can see that NavUtils.shouldUpRecreateTask(this, upIntent) returns false in both cases, and the upIntent is indeed Activity A for both cases as well. So not sure what the problem is.

@SuppressLint("NewApi")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    if (itemId == android.R.id.home) {
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                    // Navigate up to the closest parent
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        //finish();
        return true;
    } else if (itemId == R.id.wrap_menu_item) {
        wrapText();
        invalidateOptionsMenu();
        return true;
    } else {
        return super.onOptionsItemSelected(item);
    }
}
Community
  • 1
  • 1
user1159819
  • 1,549
  • 4
  • 16
  • 29
  • Forgive my ignorance, but I thought Android handled the up action automatically based upon Activity history? – Reactgular Jun 03 '13 at 15:57
  • 2
    not according to http://developer.android.com/training/implementing-navigation/ancestral.html – user1159819 Jun 03 '13 at 15:59
  • So many little things to code in Android to keep everything working. Can you share your code from `onOptionsItemSelected` in your question. That will help. – Reactgular Jun 03 '13 at 16:05
  • Have you declared activity A as the parent of activity B? – ozbek Jun 03 '13 at 17:32

1 Answers1

1

Changed Activity A property from

android:launchMode="singleInstance"

to

android:launchMode="singleTask"

resolved the issue. Makes sense because A "singleInstance" activity, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task. So the only reason Up was working before was because Activity A was "underneath" the previous activity: it gave the illusion it was going back to previous activity.

user1159819
  • 1,549
  • 4
  • 16
  • 29