【发布时间】:2014-07-01 12:58:05
【问题描述】:
我知道有很多关于处理向上导航的问题,但没有一个让我满意。
我有 3 个活动:MainActivity、BookActivity、BookChaptersActivity。 BookActivity 接收一些额外内容以正确初始化。我正在使用这种推荐的方式进行导航:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case 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);
}
return true;
}
return super.onOptionsItemSelected(item);
}
但是,当在 BookChapterActivity 中使用向上导航时,我又回到了 BookActivity,但是它在没有额外内容的情况下重新创建,所以它完全是一团糟。 我知道解决此问题的推荐方法是使用: android:launchMode="singleTop" 里面的清单。但是我不认为这是处理这个问题的正确方法。如果我想拥有多个 BookActivity 实例怎么办?
所以我问你:这种情况应该如何正确处理?
【问题讨论】:
标签: android android-activity navigation android-actionbar