【问题标题】:Pass an argument to a nested navigation graph in Jetpack Compose将参数传递给 Jetpack Compose 中的嵌套导航图
【发布时间】:2021-11-23 18:32:28
【问题描述】:

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

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

这是我目前的导航图:

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() 的方法。

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

【问题讨论】:

    标签: android android-jetpack-compose jetpack-compose-navigation


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 2022-01-21
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多