【问题标题】:Get ActionBar upIntent to act like the back button获取 ActionBar upIntent 以充当后退按钮
【发布时间】:2014-10-07 08:57:54
【问题描述】:

我正在制作这个项目,我的 MainActivity 中有 3 个固定选项卡。在选项卡 2 和 3 上,我有一些按钮可以将用户导航到新的片段活动。在片段活动中,我使用了 ActionBar.setDisplayHomeAsUpEnabled(true)。我的问题是我想让用户回到前一个片段(现在通过操作栏中的向上按钮和后退按钮完成),但我想让用户回到他们之前所在的选项卡。当我使用后退按钮向上时,我将进入我开始新片段的选项卡。但是当我在操作栏中使用 upIntent 时,我会进入选项卡 1。我该如何解决这个问题?

这是我从片段活动中找到 upIntent 操作栏按钮的代码。

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = new Intent(this, MainActivity.class);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                TaskStackBuilder.from(this)
                        // If there are ancestor activities, they should be added here.
                        .addNextIntent(upIntent)
                        .startActivities();
                finish();
            } else {
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}

【问题讨论】:

    标签: android tabs android-actionbar fragment


    【解决方案1】:

    所以我设法找到了解决方案。我将 onOptionsItemSelected 布尔值改为:

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            ClassName.this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

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