Reputation: 1679
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);
}
}
Upvotes: 1
Views: 408
Reputation: 1679
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.
Upvotes: 1