I have a pretty basic application in Jetpack Compose, where:
- First screen has a lazyColumn with many items
- Users can click on an item and be moved to a screen with details about the clicked item
- Each item is modeled using a data class that is serializable
I've tried passing in the model class with the navigation using:
val modelDataAsString = Json.encodeToString(MODEL_CLASS.serializer(), modelData)
navController.currentBackStackEntry?.arguments?.putString("KEY", modelDataAsString)
navController.navigate("ROUTE/${modelDataAsString}")
But when I do this, I get the following exception:
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/... cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78da56c6) route=main}
Now, I understood that this is because my Model class in String form is way too long (relevant SO question).
So, searching SO, I found that the solution provided was using a SharedViewModel.
I have done this and everything works well, but something doesn't feel right passing around the viewModel.
What I want to know (and understand) if this is the proper way of solving this issue. I am not looking for an opinion or a recommendation, but a definitive answer.
At the bottom line, what I want to achieve is the ability to allow the onClick method on an item in my LazyColumn move the user to a screen of the item clicked.