【发布时间】:2013-06-03 15:48:42
【问题描述】:
我正在使用此处发布的此方法在 ActionBar 中实现向上按钮:
ActionBar Up button and Navigation pattern
它工作正常,除了在一种情况下:如果活动 A 创建活动 B,然后我按向上,它将导航到 A 没问题。
但是,当我进入 Activity B,然后我切换到另一个 App,然后切换回我的 App,现在我按下 Up 按钮,它会将我导航到主屏幕而不是 Activity A。
当我调试时,我可以看到 NavUtils.shouldUpRecreateTask(this, upIntent) 在这两种情况下都返回 false,并且 upIntent 在这两种情况下也确实是 Activity A。所以不确定是什么问题。
@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);
}
}
【问题讨论】:
-
原谅我的无知,但我以为 Android 会根据 Activity 历史记录自动处理 up 操作?
-
在 Android 中编写这么多小东西来保持一切正常。您能否在您的问题中分享来自
onOptionsItemSelected的代码。这会有所帮助。 -
您是否将活动 A 声明为活动 B 的父项?
标签: android android-activity android-actionbar