FlowFan
11/05/2025, 3:46 AMorg.jetbrains.androidx.navigation:navigation3-*:1.0.0-alpha04, but I had to change it to org.jetbrains.androidx.navigation3:navigation3-* to resolve a "Could not find" error. Is this a typo in the documentation?FlowFan
11/05/2025, 9:36 AMKonstantin Tskhovrebov
11/05/2025, 9:44 AMKonstantin Tskhovrebov
11/05/2025, 9:47 AMFlowFan
11/05/2025, 10:23 AMkotlinx.serialization.SerializationException: Serializer for subclass 'Home' is not found in the polymorphic scope of 'NavKey'.
Check if class with serial name 'Home' exists and serializer is registered in a corresponding SerializersModule.
To be registered automatically, class 'Home' has to be '@Serializable', and the base class 'NavKey' has to be sealed and '@Serializable'.
at kotlinx.serialization.internal.AbstractPolymorphicSerializerKt.throwSubtypeNotRegistered(AbstractPolymorphicSerializer.kt:102)
I'm currently encountering this runtime error. Am I missing some dependency? Here are my code and dependencies:
androidx-navigation3-runtime = { group = "androidx.navigation3", name = "navigation3-runtime", version = "1.0.0-beta01" }
androidx-navigation3-ui = { group = "org.jetbrains.androidx.navigation3", name = "navigation3-ui", version = "1.0.0-alpha04" }
kotlinx-serialization-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-core", version = "1.9.0" }
kotlinSerialization = { id = "org.jetbrains.kotlin.plugin.serialization", version = "2.2.20" }
@Serializable
sealed class Screen : NavKey
@Serializable
open class Home(val id: String) : Screen()
@Serializable
open class Details(val itemId: Long) : Screen()
val config = SavedStateConfiguration {
serializersModule = SerializersModule {
polymorphic(baseClass = Screen::class) {
subclass(serializer = Home.serializer())
subclass(serializer = Details.serializer())
}
}
}
NavDisplay(
backStack = rememberNavBackStack(config, Home("1")),
entryProvider = entryProvider {
entry<Home> {
}
entry<Details> {
}
}
)Konstantin Tskhovrebov
11/05/2025, 10:31 AMFlowFan
11/05/2025, 10:46 AMpolymorphic(baseClass = Screen::class) to polymorphic(baseClass = NavKey::class), it ran successfully. What's confusing is that the sample code in androidx.navigation3.runtime.samples is written exactly like that.
@Composable
@Sampled
fun rememberNavBackStack_withSerializersModule() {
val config = SavedStateConfiguration {
// Register subtypes for open polymorphism or multiplatform use.
serializersModule = SerializersModule {
polymorphic(baseClass = Screen::class) {
subclass(serializer = Home.serializer())
subclass(serializer = Details.serializer())
}
}
}
// Pass the configuration so encoding/decoding works consistently.
rememberNavBackStack(config, Home("start"))
}Konstantin Tskhovrebov
11/05/2025, 10:49 AMIan Lake
11/05/2025, 11:01 AMrememberNavBackStack is always going to use NavKey as its base since it returns a NavBackStack<NavKey>, so polymorphic(baseClass = NavKey::class) is expected.
If you dropped down a layer and used rememberSerializable(configuration) { NavBackStack<Screen>(Home("start")) } to get a NavBackStack<Screen>, then you could use polymorphic(baseClass = Screen::class) - that would have the added benefit of only allowing your Screen subtypes into your back stackIan Lake
11/05/2025, 11:01 AMScreen type is quite useless and could simply be removed entirelyIan Lake
11/05/2025, 11:04 AM