按下返回键始终使用喷气背包导航返回起始目的地

Ber*_*hus 3 android android-fragments android-architecture-components android-jetpack-navigation

所以,我BottomNavigationView与 Jetpack Navigation 联系在一起。假设我有 4 个底部导航菜单,片段 A、B、C 和 D,A 作为起始目的地。从片段 A 转到片段 B,然后转到片段 C。然后,我按下硬件后退按钮。我期望它返回到片段 B,相反,它返回到片段 A(这是起始目的地)。

这是代码:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
navController = navHostFragment.navController

binding.navView.setupWithNavController(navController)
Run Code Online (Sandbox Code Playgroud)

我怎样才能改变这种行为?

谢谢~

编辑:我遵循 Zain 的答案,行为已经符合预期。但是,还有另一个问题。假设我有另一个片段 A1,它不是BottomNavView片段的一部分。我可以从片段 A 导航到片段 A1。下面是导航图:

<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/mobile_navigation"
    app:startDestination="@+id/navigation_a">

    <fragment
        android:id="@+id/navigation_a"
        android:name="com.example.FragmentA"
        android:label=""
        tools:layout="@layout/fragment_a">
        <action
            android:id="@+id/action_navigation_a_to_navigation_a1"
            app:destination="@id/navigation_a1"
            app:launchSingleTop="true" />
    </fragment>
    <fragment
        android:id="@+id/navigation_a1"
        android:name="com.example.FragmentA1"
        android:label=""
        tools:layout="@layout/fragment_a1" />
    <fragment
        android:id="@+id/navigation_b"
        android:name="com.example.FragmentB"
        android:label=""
        tools:layout="@layout/fragment_b" />
    <fragment
        android:id="@+id/navigation_c"
        android:name="com.example.FragmentC"
        android:label=""
        tools:layout="@layout/fragment_c" />
    <fragment
        android:id="@+id/navigation_d"
        android:name="com.example.FragmentD"
        android:label=""
        tools:layout="@layout/fragment_d" />
</navigation>
Run Code Online (Sandbox Code Playgroud)

如果我从片段 A 导航到片段 A1,然后导航到片段 B,然后按返回,它会显示正确的片段,即 A1,但 BottomNavigation 仍将片段 B 显示为活动片段,而不是片段 A。

Zai*_*ain 5

A 作为起始目的地。从片段 A 转到片段 B,然后转到片段 C。然后,我按下硬件后退按钮。我期望它返回到片段 B,相反,它返回到片段 A(这是起始目的地)。

这是导航架构组件的默认行为;BottomNavView一旦您事务到除起始目标片段之外的另一个片段,所有片段都会从返回堆栈中弹出。

为了改变这一点,文档说

默认情况下,返回堆栈将弹回到导航图的起始目的地。具有 android:menuCategory="secondary" 的菜单项不会弹出后堆栈。

因此,您需要添加android:menuCategory="secondary"除起始目标项之外的所有菜单项。在你的例子中,它们是片段 b、c 和 d 项:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/fragment_a"
        android:icon="...."
        android:title="A" />

    <item
        android:id="@+id/fragment_b"
        android:menuCategory="secondary"
        android:icon="...."
        android:title="B" />

    <item
        android:id="@+id/fragment_c"
        android:menuCategory="secondary"
        android:icon="...."
        android:title="C" />
        
    <item
        android:id="@+id/fragment_d"
        android:menuCategory="secondary"
        android:icon="...."
        android:title="D" />
        
</menu>
Run Code Online (Sandbox Code Playgroud)