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

Afzal Najam

08/01/2020, 6:42 AM
It seems like if I start an external activity (like the
ACTION_VIEW
Intent with a Uri) and then press back to come back to my compose-based app, the Composable parts aren't recomposed. The containing activity gets onStart and onResume called as expected. Does that mean I should be creating my app in onStart instead of onCreate? Or is there a way to detect this from within composables? I'm expecting a state change upon returning to the app, based on something that happens in the activity's onStop when the app loses focus. Edit: Is the right way to use
LiveData.observeAsState()
? It seems like that works.
StateFlow.collectAsState()
didn't work as well, seems like some weird race condition in my code.
j

Joost Klitsie

08/04/2020, 10:35 AM
maybe you can try to collectAsState and pass in a coroutine context like AndroidUiDispatcher.Main
a

Afzal Najam

08/04/2020, 8:11 PM
Thanks. I actually had a weird race condition where I was using collectAsState in the Scaffold composable for changing the screen BUT also using the return value of function that emitted the state to also change the screen. Once I removed the return value usage and used collectAsState in both places, that got fixed. Although, for now, I think I still prefer LiveData due to the lifecycle-awareness.
j

Joost Klitsie

08/05/2020, 7:29 AM
@Afzal Najam you can use coroutine scopes that are lifecycle aware as well. For example, if you get a lifecycleOwner.lifecycleScope you get a scope that dies with the lifecycle. Also they have helper functions like launchWhenResumed/launchWhenCreated etc. to only launch the coroutines when you are at least in that state. The AndroidUiDispatcher.Main I think synchronizes the coroutine with the choreographer of the composition, so I am not sure what happens if the app is in background but perhaps it pauses during the app being in the background automagically as I don't know/think the composition is rendered during that time
a

Afzal Najam

08/08/2020, 8:02 PM
Hmm, thanks! That's great to learn!
2 Views