I want to collect `navController.currentBackStackE...
# compose
i
I want to collect
navController.currentBackStackEntryFlow
and must to do it in CoroutineScope. I have few questions 1. Where can I read about relation of coroutines and composables 2. Where better to collect this flow? Looks like there are mechanisms in compose to handle coroutines, but i am not sure that good practice to do as i did. 3. Even if it is good practice - what is better to use as key for
LaunchedEffect
? I select activity, but it seems meaningless
d
There are better ways to write that. Checkout
collectAsState
. If you give me a copy/pastable snippet, I can show you.
i
You definitely mean
Copy code
val route by navController.currentBackStackEntryFlow
  .map { it.destination.route.orEmpty() }
  .collectAsState(initial = START_ROUTE)
Got it. Thanks
d
Pretty much. Just need to add a
remember
there.
Copy code
val route by remember(navController) {
   navController.currentBackStackEntryFlow
  .map { it.destination.route.orEmpty() }
}
  .collectAsState(initial = START_ROUTE)
i
Can you please explain what difference is?
z
If you’re looking for things to read, I’d start here: https://developer.android.com/jetpack/compose/side-effects
i
Note that there's is already a
currentBackStackEntryAsState()
method that does the right thing: https://developer.android.com/reference/kotlin/androidx/navigation/compose/package-summary#(androidx.navigation.NavCon[…]rentBackStackEntryAsState()
Copy code
val currentRoute = navController.currentBackStackEntryAsState()?.destination?.route ?: START_ROUTE
253 Views