[英]Android clear backstack when leaving item tab BottomNavigation
I am using the last navigation library (version 2.4.0) which include multiple backstacks with a bottom navigation view.我正在使用最后一个导航库(版本 2.4.0),其中包括多个带有底部导航视图的 backstack。
The default behavior is that each backstack is saved when user go back to a tab.默认行为是当用户 go 返回选项卡时保存每个 backstack。 I want to clear a backstack when the user leave a tab.
当用户离开标签时,我想清除后台堆栈。
Based on this bottom nav with 3 tabs:基于带有 3 个选项卡的底部导航:
Tab1![]() |
Tab2![]() |
Tab3 ![]() |
---|---|---|
ScreenA1![]() |
ScreenB1![]() |
ScreenC1![]() |
ScreenA2![]() |
ScreenB2![]() |
ScreenC2![]() |
From Tab1, if user:从 Tab1,如果用户:
Thanks谢谢
In your navigation file add these properties app:popUpTo
and app:popUpToInclusive
to the action which navigates to ScreenA2.在您的导航文件中,将这些属性
app:popUpTo
和app:popUpToInclusive
到导航到 ScreenA2 的操作中。
<action
android:id="@+id/action_A1_to_A2"
app:destination="@id/A2"
app:popUpTo="@+id/A1"
app:popUpToInclusive="true"/>
for more information look at this link: popUpTo example: circular logic有关更多信息,请查看此链接: popUpTo 示例:循环逻辑
Try invoking the below before you invoke the method to navigate to TAB1.在调用方法导航到 TAB1 之前尝试调用以下内容。
navController.popBackStack(R.id.ScreenA1, false);
navController.popBackStack(R.id.ScreenA1, false);
This worked for me wherein I had a similar scenario to you.这对我有用,我和你有类似的情况。
You can turn off multiple backstacks for tabs, as it was before lib version 2.4.您可以关闭选项卡的多个后台堆栈,就像在 lib 2.4 版之前一样。 For this you should change
为此,您应该更改
navView.setupWithNavController(navController)
to至
NavigationUI.setupWithNavController(navView, navController, false)
I just done by onNavigationItemSelected Listener我刚刚由 onNavigationItemSelected Listener 完成
The id in your navigation.xml and menu.xml should be different navigation.xml 和 menu.xml 中的 id 应该不同
When you handle the click action for particular item, try this当您处理特定项目的点击操作时,试试这个
R.id.home_bottom_item -> {navController.popBackStack(); navController.navigate(R.id.home_bottom_item) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.