I am working on a Jetpack Compose project with nested navigation, and I am following the official documentation on type-safe arguments in navigation, as explained in the Android Developer guide.
However, I am encountering the following error when I try to navigate using a data class inside a nested graph:
kotlinx.serialization.SerializationException: Serializer for class 'Companion' is not found. Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
Here is the relevant code where I define the route and the navigation:
Data class:
@Serializable
data class GroupDetail(val id: String)
Navigation logic: This is how I am navigating within my nested graph:
rootNavController.navigate(
GroupDetail(
id = selectedGroup.id.toString()
)
)
Nested Graph: Here’s how I set up my nested navigation graph:
fun NavGraphBuilder.groupDetailNavGraph(rootNavController: NavHostController) {
navigation<GroupDetailGraph>(
startDestination = GroupDetail
) {
composable<GroupDetail> { backStackEntry ->
val group: GroupDetail = backStackEntry.toRoute()
GroupDetailScreen(rootNavController = rootNavController)
}
}
}
Despite the GroupDetail class being marked as @Serializable, I keep getting the SerializationException.
What I've tried:
Verified that I am using the kotlinx.serialization plugin. Ensured that the GroupDetail class is annotated with @Serializable. Is there something I'm missing with serialization or passing arguments in the nested navigation? How can I resolve this issue?
I was able to resolve the issue I was facing with the kotlinx.serialization.SerializationException. After some troubleshooting, I discovered the root of the problem was related to two things:
Initialization of the route class: I needed to ensure that the route class is initialized with default or null values.
Setting the correct startDestination in the nested navigation graph: I had to use GroupDetail() as the startDestination when defining the navigation graph.
Solution Step 1: Initialize the Route Class with Default/Null Values
The first issue was with the initialization of my route. I needed to ensure that GroupDetail was initialized with null or default values when declaring the start destination in the navigation graph.
@Serializable
data class GroupDetail(val id: String? = null)
Step 2: Define Nested Graph with Correct startDestination
In my nested graph, I was missing the proper initialization of the startDestination. The solution was to ensure that GroupDetail() (with null/default values) was passed as the startDestination.
Here’s the updated navigation setup:
fun NavGraphBuilder.groupDetailNavGraph(rootNavController: NavHostController) {
navigation<GroupDetailGraph>(
startDestination = GroupDetail() // Ensure to use GroupDetail() here
) {
composable<GroupDetail> { backStackEntry ->
val group: GroupDetail = backStackEntry.toRoute()
GroupDetailScreen(rootNavController = rootNavController)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With