将 Navigation UI 与 Navigation Drawer 一起使用时,片段不会添加到后台堆栈

Fragments aren´t added to backstack when using Navigation UI with Navigation Drawer

我正在使用 Android 导航组件创建带有导航抽屉的应用程序。但是,如果通过导航抽屉更改当前片段,然后将应用程序总是 returns 返回到导航图的起始片段。 我只能找到如何在使用导航组件时从 Backstack 中删除片段的解决方案,但找不到如何添加它们的方法。

我已经在 NavController 的 AppBarConfiguration 中添加了其他片段作为根片段,但这并没有改变行为。 在应用程序的其他部分使用标准 navController.navigate() 时,片段会正确添加到 Backstack。只有当我从 Navigation Drawer 切换 Fragments 时,它们才不会添加到 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>

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

非常感谢

我已经成功生成了所需的行为。我只是忽略了文档的相关部分。如 documentation of the NavigationUI 中所述,您需要将 android:menuCategory = "secondary" 添加到导航视图菜单中的菜单项。通过在 XML 文件中添加此行,当切换到相应的 Fragment 时,不会弹出后台堆栈,因此当按下返回时,应用程序 returns 会跳转到前一个 Fragment。