【问题标题】:Action Bar UP navigation Activity to Fragment操作栏向上导航活动到片段
【发布时间】:2016-05-25 23:16:07
【问题描述】:

我正在开发一个带有导航抽屉的应用程序。虽然我以前没有任何片段经验,但我可以成功实现抽屉导航。现在我的问题是,我想要一个操作栏后退按钮。我以前尝试过这个活动。但现在的问题是我有一个片段,里面有一个点击事件。当用户单击按钮时,它会转到其中包含列表视图的活动。现在我想使用操作栏向上按钮返回上一个片段。我该怎么做?当我按下手机上的返回按钮时,它工作正常。

我在列表视图活动中实现了 onOptionsItemSelected,如下所示。当我按下操作栏向上按钮时,它会进入登录窗口。不是我想去的地方。

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    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);
}

有人可以就这个问题提供一些帮助吗?

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    我认为在这篇文章中:Using ActionBar Up Navigation in master/detail fragments 你可以找到这个问题的答案。

    我认为您必须使用 FragmentManager 而不是 Activity。

    【讨论】:

    • 是的,它工作了.. 谢谢你的帮助.. 我有一个不同的问题要问你.. 如果你能告诉我,我可以在适配器类中使用 assync 任务吗?因为我的应用程序在运行异步任务时有时会崩溃。希望第一次和第二次尝试可以正常工作。但是当我尝试多次运行某些任务时,它崩溃了:(
    • 很高兴听到它有效。最适合你的另一个问题是,你为此开始了另一个话题。使用 Code、ErrorLog 和进程(崩溃的地方)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多