将参数传递给Jetpack Compose中的嵌套导航图 [英] Pass an argument to a nested navigation graph in Jetpack Compose

查看:18
本文介绍了将参数传递给Jetpack Compose中的嵌套导航图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docs我看到您可以像这样嵌套导航图:

NavHost(navController, startDestination = "home") {
    ...
    // Navigating to the graph via its route ('login') automatically
    // navigates to the graph's start destination - 'username'
    // therefore encapsulating the graph's internal routing logic
    navigation(startDestination = "username", route = "login") {
        composable("username") { ... }
        composable("password") { ... }
        composable("registration") { ... }
    }
    ...
}

我在想,如何在路由中传递参数,并使其可用于NAV图中的所有组合?

这是我当前的导航图表:

navigation(
    // I'd like to grab this parameter
    route = "dashboard?classId={classId}",
    startDestination = Route.ScreenOne.route) {
    composable(Route.ScreenOne.route) {
        // And then pass the parameter here, or to any composable below
        ScreenOne(classId)
    }
    composable(Route.ScreenTwo.route) {
        ScreenTwo()
    }
    composable(Route.ScreenThree.route) {
        ScreenThree()
    }
}

我基本上是在尝试避免在每条可组合的路线上单独设置classID导航参数。我没有看到像在composable()中那样将参数列表传递给navigation()的方法。

可能我所描述的是不可能的,但期待大家的想法!

推荐答案

您可以从子可组合项访问图形参数:

navController.getBackStackEntry("dashboard?classId={classId}").arguments?.getString("classId")

这篇关于将参数传递给Jetpack Compose中的嵌套导航图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆