从片段导航时底部导航栏出现故障

发布于 2025-01-15 03:22:07 字数 1412 浏览 3 评论 0原文

我将底部导航栏与导航组件一起使用

为了使这两个组件协同工作,我调用了:

bottomNavigationView.setupWithNavController(navController)

一切都按预期工作,除非我从片段内部而不是底部导航栏导航

在此处输入图像描述

"查看全部” 从底部导航栏打开与“报告”相同的片段

binding.viewAllScansTv.setOnClickListener {
    val action = MainFragmentDirections.actionMainFragmentToReportsFragment()
    navController.navigate(action)
}

单击“查看全部”后,片段将打开,“报告”按钮将被选中,但是,导航回“主页”不再起作用

如何修复这种奇怪的行为?

导航图:

<navigation app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.package.name.ui.main.MainFragment"
        android:label="MainFragment"> 
        <action                android:id="@+id/action_mainFragment_to_reportsFragment"
            app:destination="@id/reportsFragment" />
    </fragment>
</navigation>

底部导航菜单:

<menu>
    <item
        android:id="@+id/mainFragment"
        android:title="Home"/>    
    <item
        android:id="@+id/reportsFragment"
        android:title="Reports"/>
    <item
        android:id="@+id/settingsFragment"
        android:title="My account"/>
</menu>

I'm using the bottom navigation bar with the navigation component

To make the two components work together I called:

bottomNavigationView.setupWithNavController(navController)

Everything works as expected except when I navigate from inside a fragment instead of the bottom navigation bar

enter image description here

"View all" opens the same fragment as "Reports" from the bottom navigation bar

binding.viewAllScansTv.setOnClickListener {
    val action = MainFragmentDirections.actionMainFragmentToReportsFragment()
    navController.navigate(action)
}

After clicking on "View all", the fragment is opened, the "Reports" button gets selected, however, navigating back "Home" does not work anymore

How can I fix this weird behavior?

The nav graph:

<navigation app:startDestination="@id/mainFragment">
    <fragment
        android:id="@+id/mainFragment"
        android:name="com.package.name.ui.main.MainFragment"
        android:label="MainFragment"> 
        <action                android:id="@+id/action_mainFragment_to_reportsFragment"
            app:destination="@id/reportsFragment" />
    </fragment>
</navigation>

The bottom navigation menu:

<menu>
    <item
        android:id="@+id/mainFragment"
        android:title="Home"/>    
    <item
        android:id="@+id/reportsFragment"
        android:title="Reports"/>
    <item
        android:id="@+id/settingsFragment"
        android:title="My account"/>
</menu>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

地狱即天堂 2025-01-22 03:22:07

正如@ianhanniballake在评论中提到的那样,发布了类似的问题这里

我最终做的是替换

val action = MainFragmentDirections.actionMainFragmentToReportsFragment()
navController.navigate(action)

所以

val item = mainBottomNavigationView.menu.findItem(R.id.reportsFragment)
NavigationUI.onNavDestinationSelected(item, navController)

基本上我使用 NavigationUI API 进行导航,以便它正确跟踪返回堆栈。 BottomNavigationView 在内部使用相同的 NavigationUI API

As @ianhanniballake mentioned in a comment, a similar question was posted here

What I ended up doing was replacing

val action = MainFragmentDirections.actionMainFragmentToReportsFragment()
navController.navigate(action)

with

val item = mainBottomNavigationView.menu.findItem(R.id.reportsFragment)
NavigationUI.onNavDestinationSelected(item, navController)

So basically I used the NavigationUI API to navigate so that it correctly tracks the back stack. The same NavigationUI API is being used by the BottomNavigationView internally

飘过的浮云 2025-01-22 03:22:07

您当前的答案很好,但如果您需要传递参数,它将无法工作,因此请使用此

在导航 XML 中将这些行添加到操作中

app:launchSingleTop="true"
app:popUpTo="@+id/main_navigation"
app:popUpToInclusive="true"

并确保 app:popUpTo="@+id/main_navigation"与您的导航 xml 具有相同的 id

所以最终的操作应该如下所示:

     <action
            android:id="@+id/action_cameraFragment_to_searchFragment"
            app:destination="@id/searchFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_navigation"
            app:popUpToInclusive="true"/>

并且通常使用该操作进行导航

val action = CameraFragmentDirections.actionCameraFragmentToSearchFragment()
findNavController().navigate(action)

Your current answer is fine but if you need to pass arguments it wont work, So use this

In the Navigation XML add these lines to the action

app:launchSingleTop="true"
app:popUpTo="@+id/main_navigation"
app:popUpToInclusive="true"

And make sure app:popUpTo="@+id/main_navigation" has the same id as your navigation xml

So the final action should look like:

     <action
            android:id="@+id/action_cameraFragment_to_searchFragment"
            app:destination="@id/searchFragment"
            app:launchSingleTop="true"
            app:popUpTo="@+id/main_navigation"
            app:popUpToInclusive="true"/>

And Navigate normally using the action

val action = CameraFragmentDirections.actionCameraFragmentToSearchFragment()
findNavController().navigate(action)
甜尕妞 2025-01-22 03:22:07

在任何视图上设置点击侦听器应该具有与用户点击底部导航中的相应项目相同的效果。因此,您需要在 BottomNavigationView 上调用 setSelectedItemId()

val mainBottomNav =
                activity?.findViewById<BottomNavigationView>(R.id.homeBottomNavigation)
            mainBottomNav?.selectedItemId = R.id.baseHomeFragment

Setting a click listener on any view should have the same effect as if the user taps the corresponding item in the bottom navigation. So you need to call setSelectedItemId() on the BottomNavigationView.

val mainBottomNav =
                activity?.findViewById<BottomNavigationView>(R.id.homeBottomNavigation)
            mainBottomNav?.selectedItemId = R.id.baseHomeFragment
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文