【问题标题】:Implementing ActionBar Up button实现 ActionBar 向上按钮
【发布时间】: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


【解决方案1】:

更改了 Activity A 属性
android:launchMode="singleInstance"

android:launchMode="singleTask"

解决了这个问题。有意义,因为“单一实例”活动不允许其他活动成为其任务的一部分。这是任务中的唯一活动。如果它启动另一个活动,则该活动将分配给不同的任务。所以 Up 之前工作的唯一原因是因为 Activity A “低于”之前的 Activity:它给人的错觉是它正在回到之前的 Activity。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多