JetPack组成:底栏和嵌套导航
我已经在带有2个目的地的JetPack组成应用程序上设置了一个项目的底线。我试图关注Google的样本。
因此,例如,它看起来像这样:
@Composable
fun ExampleBottomBar(navController: NavHostController) {
val items = listOf(
BottomNavigationGraph.Graph1,
BottomNavigationGraph.Graph2
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
BottomNavigation {
items.forEach { screen ->
BottomNavigationItem(
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
icon = { Icon(imageVector = screen.icon, contentDescription = null) },
label = { Text(stringResource(screen.label)) }
)
}
}
}
这一切都很好,我能够在两个目的地之间导航。但是,当我从Graph-1 Screen-2
到Graph-2 Screen-2-2
时,它会毫无任何问题导航。但是,当我按Graph-1
上的底部选项卡时,它不起作用。底部选项卡似乎不再可单击。 因此,我尝试了此解决方案以导航到嵌套屏幕(不同的图):
navigate(route) {
launchSingleTop = true
restoreState = false
popUpTo(graph.findStartDestination().id) {
saveState = true
}
}
现在,此解决方案可完美。现在,当我单击graph-1
的底部选项卡时,它会切换并保留graph-1 screen-1-2
的状态,但是当我从graph- 2 Screen-2
,它将我直接导航到Graph-1 Screen-1
,它基本上为Graph> Graph-1 Screen-1-1
创建了另一个堆栈。 因此,基本上我想要的是在后按下或底部按钮上保留Graph-1 Screen-2 的状态。
我是做错了什么还是这是一个错误? 有人可以在这里帮我吗? 提前致谢。
I have setup a Bottom Bar in project on Jetpack Compose app with 2 destinations. I have tried to follow the samples from Google.
So for example it looks something like this:
@Composable
fun ExampleBottomBar(navController: NavHostController) {
val items = listOf(
BottomNavigationGraph.Graph1,
BottomNavigationGraph.Graph2
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
BottomNavigation {
items.forEach { screen ->
BottomNavigationItem(
onClick = {
navController.navigate(screen.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
icon = { Icon(imageVector = screen.icon, contentDescription = null) },
label = { Text(stringResource(screen.label)) }
)
}
}
}
This all works fine and I'm able to navigate between the two destinations. However, When I navigate from Graph-1 Screen-2
to Graph-2 Screen-2
, It navigates without any issue. But when I press Bottom Tab on Graph-1
, it doesn't work. The Bottom Tab seems to be not clickable anymore.
So I tried this solution to navigate to nested screen (different graph):
navigate(route) {
launchSingleTop = true
restoreState = false
popUpTo(graph.findStartDestination().id) {
saveState = true
}
}
Now this solution works perfectly. Now when I click on Bottom Tab for Graph-1
it switches and retain state for Graph-1 Screen-2
, But when I press back button from Graph-2 Screen-2
, it navigates me directly to Graph-1 Screen-1
which creates basically another stack for Graph-1 Screen-1
.
So basically what I want is to retain state for Graph-1 Screen-2
on back press or BottomTab click.
Am I doing something wrong or is this a bug?
Can someone help me out here ?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就我而言,设置
savestate
和Restorestate
从navoptionsbuilder
atfalse
有助于解决问题。In my case, setting
saveState
andrestoreState
fromNavOptionsBuilder
asfalse
helped to resolve the issue.