简体   繁体   中英

Pass an argument to a nested navigation graph in Jetpack Compose

From the docs , I see you can nest navigation graphs like so:

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

I am wondering, how would one pass an argument in the route, and make that available to all composables inside the nav graph?

Here's my current nav graph:

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()
    }
}

I am basically trying to avoid setting the classId navigation argument individually on each composable route. I didn't see a way to pass a list of arguments to navigation() like you can in a composable() .

It might be that what I am describing isn't possible, but looking forward to anyone's thoughts!

The can access the graph arguments from child composables:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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