Hi, Does someone know any workarounds for this nav...
# compose
j
Hi, Does someone know any workarounds for this nav3 backstack polymorphic definitions. Because I have to update them every time here's an example.
Copy code
val backStack = rememberNavBackStack(
        configuration = SavedStateConfiguration {
            serializersModule = SerializersModule {
                polymorphic(NavKey::class) {
                    subclass(Navigation.Home::class)
                    subclass(Navigation.Details::class)
                }
            }
        },
        Navigation.Home,
    )
Is there any other that it will use every sub classes from sealed interface or sealed class ?
i
👀 3
r
Simple, don't use
rememberNavBackStack
and use
rememberSerializable
because the first one was designed for easy adopt and your usecase doesn't fit it. You can see the source code of rembebrNavBackStack and see is a shortcut for serializable +
NavBackSerializer
Important you need to use
sealed class
and not
sealed interface
until you only target android/jvm platforms (in that case, serialization use reflection to obtain the serializer) Sample:
Copy code
val backStack = rememberSerializable(
        serializer = NavBackStackSerializer(VibrionRoutes.serializer()),
    ) {
        NavBackStack(Home)
    }
Copy code
import androidx.navigation3.runtime.NavKey
import kotlinx.serialization.Serializable

@Serializable
sealed class VibrionRoutes : NavKey {
    @Serializable
    data object Home : VibrionRoutes()

    @Serializable
    data object Settings : VibrionRoutes()

    @Serializable
    data object Libraries : VibrionRoutes()
}
👀 1