JetPack组成:底栏和嵌套导航

发布于 2025-02-10 14:49:58 字数 1788 浏览 1 评论 0原文

我已经在带有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-2Graph-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 技术交流群。

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

发布评论

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

评论(1

最冷一天 2025-02-17 14:49:59

就我而言,设置savestateRestorestatenavoptionsbuilder at false有助于解决问题。

In my case, setting saveState and restoreState from NavOptionsBuilder as false helped to resolve the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文