1

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

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 导航参数。我没有看到一种方法可以将参数列表传递navigation()composable().

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

4

1 回答 1

0

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

navController.getBackStackEntry("dashboard?classId={classId}").arguments?.getString("classId")
于 2021-11-24T03:56:47.333 回答