https://kotlinlang.org logo
#compose
Title
# compose
f

Fanilog

05/18/2022, 6:44 PM
I’ve a question related to Compose and Coroutine. Would it be an issue doing something like this
Copy code
//TOP level composable of the app
with(LocalLifecycleOwner.current) {
    lifecycleScope.launch {
        navigationManager.navigationEvents
            .flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
            .collect { navigationDirection ->
            //Listening to navigation event
            navController.navigate()...          
            }
    }
Or should I do it in LaunchEffect ? Since Compose function can execute in any order, I wanted to avoid to have it a LaunchEffect and use a replay parameter of a Flow. (eg: Sending a event before the collection,)
m

myanmarking

05/18/2022, 7:02 PM
since that does not use remember, yes. that is a problem
f

Fanilog

05/18/2022, 7:05 PM
Ok so if I get, using the block inside the remember with the Locallifecycle.current as key should be sufficient ? And what would be the difference doing this in LaunchEffect(Unit)?
z

Zach Klippenstein (he/him) [MOD]

05/19/2022, 6:27 PM
Your concern about missing navigation events is exactly why your compose ui layer should not be the one handling such events. Navigation events should update some state, and that state should be consumed by composables.