alvaro_se
alvaro_se

Reputation: 365

OnDestinationChangedListener with Typesafe navigation: Jetpack Compose

I'm using Jetpack Compose navigation with type-safe routes. I have these screens:

sealed class NavRoute {
    @Serializable
    object Home : NavRoute()
    
    @Serializable
    object Login: NavRoute()
    
    @Serializable
    data class OrderDetail(val orderId: Int) : NavRoute()
}

Is there any way to cast navDestination to my TypeSafe NavRoute class?

DisposableEffect(navController) {
    val listener = NavController.OnDestinationChangedListener { _, navDestination, _ ->
        Log.d("ScreenName", "Destination changed to ${navDestination.route}")
    }
    navController.addOnDestinationChangedListener(listener)
    onDispose {
        navController.removeOnDestinationChangedListener(listener)
    }
}

Upvotes: 1

Views: 16

Answers (0)

Related Questions