从片段导航时底部导航栏出现故障
我将底部导航栏与导航组件一起使用
为了使这两个组件协同工作,我调用了:
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
"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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如@ianhanniballake在评论中提到的那样,发布了类似的问题这里
我最终做的是替换
所以
基本上我使用
NavigationUI
API 进行导航,以便它正确跟踪返回堆栈。BottomNavigationView
在内部使用相同的NavigationUI
APIAs @ianhanniballake mentioned in a comment, a similar question was posted here
What I ended up doing was replacing
with
So basically I used the
NavigationUI
API to navigate so that it correctly tracks the back stack. The sameNavigationUI
API is being used by theBottomNavigationView
internally您当前的答案很好,但如果您需要传递参数,它将无法工作,因此请使用此
在导航 XML 中将这些行添加到操作中
并确保
app:popUpTo="@+id/main_navigation"
与您的导航 xml 具有相同的 id所以最终的操作应该如下所示:
并且通常使用该操作进行导航
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
And make sure
app:popUpTo="@+id/main_navigation"
has the same id as your navigation xmlSo the final action should look like:
And Navigate normally using the action
在任何视图上设置点击侦听器应该具有与用户点击底部导航中的相应项目相同的效果。因此,您需要在
BottomNavigationView
上调用setSelectedItemId()
。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 theBottomNavigationView
.