How to do `NestedNavigation` (jetpack navigation) ...
# compose
c
How to do
NestedNavigation
(jetpack navigation) with arguments? How would I pass
id
to the
login
nav graph?
Copy code
fun NavGraphBuilder.loginGraph(navController: NavController) {
    navigation(
        startDestination = "username", 
        route = "login/{id}", 
        arguments = listOf(navArgument("id") { type = NavType.StringType }),
    ) {
        composable("username") { ... }
        composable("password") { ... }
        composable("registration") { ... }
    }
}
j
You’re doing it correctly. Just navigate to the graph:
navController.navigate("login/123")
. You can get the argument either from the `startDestination`’s
arguments
, or from the graph’s arguments:
Copy code
navController.getBackStackEntry("login/{id}").arguments.getString("id")
c
But
arguments
is not a valid parameter for
navigation
. This is why I was confused how to do it
There is a similar question asking how to add
deeplink
to nested navigation graph https://stackoverflow.com/questions/69906721/jetpack-compose-deep-link-into-nested-navigation
j
Oh, are you using the
navigation
function from Accompanist? It’s missing these arguments, but the function from
androidx.navigation.compose
has them
c
Yes from accompanist. Does the regular function from jetpack navigation have
arguments
param?
j
It works fine with transition animations, just make sure you use the
composable
functions from
com.google.accompanist.navigation.animation
Yes, the regular one has it:
Copy code
public fun NavGraphBuilder.navigation(
    startDestination: String,
    route: String,
    arguments: List<NamedNavArgument> = emptyList(),
    deepLinks: List<NavDeepLink> = emptyList(),
    builder: NavGraphBuilder.() -> Unit
)
c
Ok. Will check it out. Thanks
i
Note you don't need them as parameters at all - that is pure convenience. The lambda parameter is the scope that lets you set all of those extra parameters:
Copy code
fun NavGraphBuilder.loginGraph(navController: NavController) {
     navigation(
         startDestination = "username", 
         route = "login/{id}"
     ) {
         // Inside the lambda, any of the
         // NavGraphBuilder extensions can
         // be called directly
         navArgument("id") { type = NavType.StringType })
         composable("username") { ... }
         composable("password") { ... }
         composable("registration") { ... }
     }
 }
today i learned 2