繁体   English   中英

将参数传递给 Jetpack Compose 中的嵌套导航图

[英]Pass an argument to a nested navigation graph in Jetpack Compose

文档中,我看到您可以像这样嵌套导航图:

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") { ... }
    }
    ...
}

我想知道,如何在路线中传递一个参数,并将其提供给导航图中的所有可组合项?

这是我当前的导航图:

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()中那样将 arguments 列表传递给navigation() ) 的方法。

可能我所描述的是不可能的,但期待任何人的想法!

可以从子可组合项访问图 arguments:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM