I noticed in the release notes the dependency is l...
# compose
f
I noticed in the release notes the dependency is listed as
org.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?
👌 2
1
I'm now encountering: "Could not find org.jetbrains.androidx.navigation3navigation3 runtime1.0.0-alpha04." Only the navigation3-ui module appears to be available. Am I doing something wrong here?
k
There is no org.jetbrains.androidx.navigation3:navigation3-runtime library Only androidx.navigation3:navigation3-runtime
f
Copy code
kotlinx.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:
Copy code
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> {
        }
    }
)
k
f
When I changed
polymorphic(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.
Copy code
@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"))
}
k
cc @Ian Lake would be good to add it in the Nav3 doc
i
Ah, yep.
rememberNavBackStack
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 stack
👍🏾 1
👍 2
Since otherwise your
Screen
type is quite useless and could simply be removed entirely
We'll be fixing the sample in http://r.android.com/3838540 though