[英]Implementing ActionBar Up button
我正在使用此处发布的此方法在ActionBar中实现“向上”按钮:
除非在一种情况下,否则它可以正常工作:如果活动A创建活动B,然后按Up,它将导航到A没问题。
但是,当我进入活动B,然后切换到另一个应用程序,然后又切换回我的应用程序时,现在按下向上按钮,它将导航我到主屏幕,而不是活动A。
当我调试时,我可以看到NavUtils.shouldUpRecreateTask(this,upIntent)在两种情况下都返回false,并且在两种情况下upIntent的确是ActivityA。 所以不确定是什么问题。
@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:launchMode="singleInstance"
至
android:launchMode="singleTask"
解决了问题。 之所以有意义,是因为“ singleInstance”活动不允许任何其他活动成为其任务的一部分。 这是任务中唯一的活动。 如果启动另一个活动,则该活动将分配给其他任务。 因此Up之前工作的唯一原因是因为活动A“位于”先前活动的“下方”:它给人一种幻觉,使它可以回到先前的活动。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.