操作栏向上导航 Activity 到 Fragment

Gis*_*ana 5 android android-fragments android-actionbar

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

我已经在我的列表视图活动中实现了 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);
}
Run Code Online (Sandbox Code Playgroud)

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

Man*_* Zi 1

我认为在这篇文章中:在主/细节片段中使用 ActionBar 向上导航,您可以找到这个问题的答案。

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