Using the new Compose Type-Safe Navigation. How ca...
# compose
t
Using the new Compose Type-Safe Navigation. How can you check if the currentBackstackEntry is a specific route?
Copy code
val backStackEntry by navController.currentBackStackEntryAsState()

LaunchedEffect(backStackEntry) {
    val route: Route = backStackEntry?.toRoute() // Always returns null, even when backStackEntry route is not null
}
I currently am using a workaround using the qualified name:
Copy code
val route = backStackEntry?.destination?.route == Screen.Profile::class.qualifiedName
Though I would prefer a better method. I personally would expect that if I do toRoute() I could do a check like
route is Screen.Profile
but since toRoute always returns null this check will always fail The way I have build my routes:
Copy code
sealed interface Screen {
    sealed interface Auth: Screen {
        @Serializable
        data object Login : Auth

        @Serializable
        data object Register: Auth
    }

    @Serializable
    data object Profile: Screen
}
t
Damn overlooked that one because Android Studio was continiously importing another one that wasn't a generic
Thanks 🙂
👍 1
k
@Tom Truyen This does't seem to work with NavGraphs or inheritance in my app.
hasRoute<Screeb.Auth>()
wouldn't work for example.
t
Do you have a code snippet?
189 Views