首页 > 解决方案 > 将 Navigation UI 与 Navigation Drawer 一起使用时,片段不会添加到 backstack

问题描述

我正在使用 Android 导航组件创建一个带有导航抽屉的应用程序。但是,如果如果通过 Navigation Drawer 更改当前 Fragment 然后按回,则应用程序始终会返回到 Navigation Graph 的起始 Fragment。我只能找到解决方案如何在使用导航组件时从 Backstack 中删除 Fragments,而不是如何添加它们。

我已经在 NavController 的 AppBarConfiguration 中添加了其他片段作为根片段,但这不会改变行为。在应用程序的其他部分使用标准navController.navigate()时,片段会正确添加到 Backstack。只有当我从导航抽屉切换片段时,它们才不会添加到 Backstack。

我也尝试过改变片段之间的动作(例如popUpTopopUpToInclusive),但导航组件似乎没有使用这些,因为它没有改变任何东西。

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstaceState);
    setContentView(R.layout.activity_main);

    DrawerLayout drawer_layout = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);

    navController = Navigation.findNavController(this, R.id.nav_host_fragment);

    appBarConfiguration = new AppBarConfiguration.Builder(R.id.homeFragment, R.id.Fragment1,
                          R.id.Fragment2, R.id.Fragment3).setDrawerLayout(drawer_layout).build();

    NavigationUI.setupWithNavController(navigationView, navController);
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

@Override
public boolean onSupportNavigateUp() {
    navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, appBarConfiguration) || super.onSupportNavigateUp();
}

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="package.fragments.homeFragment"
        android:label="@string/home_fragment_label"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/Fragment1"
        android:name="package.fragments.Fragment1"
        android:label="@string/fragment1_label"
        tools:layout="@layout/fragment1" />
    <fragment
        android:id="@+id/Fragment2"
        android:name="package.fragments.Fragment2"
        android:label="@string/fragment2_label"
        tools:layout="@layout/fragment2" />
    <fragment
        android:id="@+id/Fragment3"
        android:name="package.fragments.Fragment3"
        android:label="@string/fragment3_label"
        tools:layout="@layout/fragment3" />
</navigation>

当从Fragment1to切换Fragment2然后按回时,我希望应用程序返回到Fragment1,但应用程序会重定向到homeFragment. 有什么办法可以防止 Navigation Component 总是返回到起始 Fragment 而不是之前的 Fragment?

非常感谢

标签: androidandroid-fragmentsnavigation-drawerandroid-jetpackandroid-architecture-navigation

解决方案


我已经设法产生所需的行为。我只是忽略了文档的相关部分。如NavigationUI 的文档中所述,您需要将其添加android:menuCategory = "secondary"到 Navigation View 菜单中的菜单项中。通过在 XML 文件中添加这一行,在切换到相应的 Fragment 时不会弹出 backstack,因此应用程序会在按下返回时返回到前一个 Fragment。


推荐阅读