使用导航抽屉时,使用导航UI Fragments不会添加到返回堆栈中

3
我正在使用Android Navigation Component创建一个带有侧边栏导航的应用程序。然而,如果通过导航抽屉更改当前Fragment,然后按返回键,该应用程序总是返回到导航图的起始Fragment。 我只能找到在使用Navigation Component时如何从Backstack中删除Fragments的解决方案,但没有找到如何添加它们的方法。
我已经将其他Fragments作为NavController中AppBarConfiguration的根Fragments添加了进去,但这并没有改变其行为。 当在应用程序的其他部分使用标准的navController.navigate()时,Fragments会被正确地添加到Backstack中。只有当我从导航抽屉中切换Fragments时,它们才不会被添加到Backstack中。
我还尝试了更改Fragments之间的操作(例如popUpTopopUpToInclusive),但似乎Navigation Component没有使用这些,因为它并没有改变任何内容。 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

是否有任何方法可以防止导航组件总是返回到起始片段而不是上一个片段?

非常感谢。

1个回答

4
我已经成功生成了所需的行为。我只是忽略了文档中相关的部分。如 导航UI文档 所述,您需要在导航视图菜单中的菜单项中添加 android:menuCategory = "secondary"。通过在XML文件中添加此行,当切换到相应的片段时,后退栈不会弹出,因此在按下返回键时,应用程序将返回到先前的片段。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接