how can I listen for route changes in compose navi...
# compose
s
how can I listen for route changes in compose navigation and check for a specific route ?
k
NavController.currentBackStackEntryAsState()
might help. What are you trying to observe?
s
trying to observe current destination route composable to hide show some UI elements based of a route
i
Yep, that's exactly what
currentBackStackEntryAsState()
is for. You can see the example in the docs on how to get the route: https://developer.android.com/jetpack/compose/navigation#bottom-nav
s
okay, how can I get current String “route” from this State holder so I can perform some logic ?
t
Copy code
@Composable
fun NavHostController.currentRoute(): String? {
    val navBackStackEntry by currentBackStackEntryAsState()
    return navBackStackEntry?.arguments?.getString(KEY_ROUTE)
}
😄 1
s
awesome, thanks
i
Yep, those are the two lines in that link I put above
🙏 1