【问题标题】:Fragments aren´t added to backstack when using Navigation UI with Navigation Drawer将 Navigation UI 与 Navigation Drawer 一起使用时,片段不会添加到 backstack
【发布时间】:2019-08-10 15:15:53
【问题描述】:

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

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

我也尝试过更改 Fragments 之间的操作(例如 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?

非常感谢

【问题讨论】:

    标签: android android-fragments navigation-drawer android-jetpack android-architecture-navigation


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2014-08-27
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多