Hello people! :slightly_smiling_face: I am using J...
# navigation-architecture-component
j
Hello people! 🙂 I am using Jetpack Compose and navigation component, I was wondering: can we pass a type safe argument to a nested navigation graph? In theory,
navigation<MyGraph> {}
supports the same type safe objects. I was hoping we can simply access the back stack entry for that and then read out the data we passed (more in the comment). Is this possible?
✅ 1
Copy code
@Serializable
data class MyGraph(val text: String) {

    @Serializable
    data object Start

}

fun NavGraphBuilder.registerTestGraph(navController: NavController) {
    navigation<MyGraph>(
       startDestination = MyGraph.Start,
    ) {
       composable<MyGraph.Start> {
          val route = remember { navController.getBackStackEntry<MyGraph>().toRoute<MyGraph>() }
          Text(route.text)
       }
    }
}
But if I call this:
navController.navigate(MyGraph("test"))
it will crash at the line where I try to resolve the route, as the field
text
cannot be found
The thing I want to achieve is to pass the data somehow into my start destination.
Okay seems like I solved it 🙂
Copy code
@Serializable
data class MyGraph(val text: String) {

    @Serializable
    data class Start(val text: String)

}

fun NavGraphBuilder.registerTestGraph(navController: NavController) {
    navigation<MyGraph>(
       startDestination = MyGraph.Start::class,
    ) {
       composable<MyGraph.Start> { entry ->
          val route = remember { entry.toRoute<MyGraph.Start>() }
          Text(route.text)
       }
    }
}
Simply add the parameters to both the graph and the destination and then they get passed 😄 amazing