Is there a way, to get current `NavHostController`...
# compose
p
Is there a way, to get current
NavHostController
route as
KClass
? For example:
Copy code
private sealed interface Destination {
    @Serializable data object Home : Destination
    @Serializable data object Settings : Destination
}

val controller = rememberNavController()
NavHost(navController = controller, startDestination = Destination.Home) {
    composable<Destination.Home> {
        // TODO
    }
    composable<Destination.Settings> {
        // TODO
    }
}

LaunchedEffect(controller.currentDestination) {
    // How to get current route of type [Destination]?
}
i
The pattern you're showing doesn't actually tell us what problem you are trying to solve, so this might be more of an XY Problem kind of situation, but the API for determining what KClass you are on is `hasRoute`: https://kotlinlang.slack.com/archives/C04TPPEQKEJ/p1715603846612999?thread_ts=1715593577.533989&amp;cid=C04TPPEQKEJ
p
I'm trying to select item in bottom navigation bar. I see
hasRoute()
can be used to solve my problem, thanks. I was trying to do it a bit differently. I basically wanted to do something like following, while keeping
NavHostController
source of truth for current
Destination
Copy code
private sealed interface Destination {
    @Serializable data object Home : Destination
    @Serializable data object Settings : Destination
}

val controller = rememberNavController()
NavHost(navController = controller, startDestination = Destination.Home) {
    composable<Destination.Home> {
        // TODO
    }
    composable<Destination.Settings> {
        // TODO
    }
}

val currentDestination = controller.somehowGetCurrentDestination()
MyBottomNavigation(
    currentDestination = currentDestination,
    onSelectDestination = controller::navigate,
)
i
If you look at our bottom nav example in the docs, you'll note it uses the type safe APIs: https://developer.android.com/develop/ui/compose/navigation#bottom-nav
There's no reason you couldn't move the code that is inline there for
selected
and
onClick
into a style close to what you have for
currentDestination
and
onSelectDestination
p
ah, okay, I missed that. Thank you!