gpt4 book ai didi

android - 使用喷气背包导航按返回总是返回起始目的地

转载 作者:行者123 更新时间:2023-12-04 23:55:46 25 4
gpt4 key购买 nike

因此,我将 BottomNavigationView 与 Jetpack Navigation 绑定(bind)在一起。假设我有 4 个底部导航菜单, fragment A、B、C 和 D,A 作为起始目的地。从 Fragment A 转到 Fragment B,然后转到 Fragment C。然后,我按下了硬件后退按钮。我希望它返回到 fragment B,相反,它返回到 fragment A(这是开始目的地)。

这是代码:

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

binding.navView.setupWithNavController(navController)

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

谢谢~

编辑:我遵循了 Zain 的回答,并且行为已经符合预期。但,还有一个问题。假设我有另一个 fragment A1,它不是 BottomNavView fragment 的一部分。我可以从 fragment A 导航到 fragment 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>

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

最佳答案

A as the start destination. From Fragment A, I go to Fragment B, and then to Fragment C. Then, I pressed the hardware back button. I expected it to return to fragment B, instead, it return to Fragment A (which is the start destination).

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

为了改变这一点,the documentation says :

By default, the back stack will be popped back to the navigationgraph's start destination. Menu items that haveandroid:menuCategory="secondary" will not pop the back stack.

因此,您需要在除起始目标项之外的所有菜单项中添加 android:menuCategory="secondary"。在您的例子中,它们是 fragment 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>

关于android - 使用喷气背包导航按返回总是返回起始目的地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69819900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com