shahroz
09/10/2021, 11:22 AMShakil Karim
09/10/2021, 11:32 AMshahroz
09/10/2021, 11:38 AMlifecycleScope.launchWhenResumed {
viewModel.getData().collect {
// ...
}
Marko Novakovic
09/10/2021, 11:48 AMFlow.flowWithLifecycle
is solution for what you needshahroz
09/10/2021, 11:49 AMMarko Novakovic
09/10/2021, 11:50 AMshahroz
09/10/2021, 12:28 PMUnresolved reference: flowWithLifecycle
Do you have any clue what dependency i might be missing? got all of the lifecyle ones as well as compose lifecycle oneshahroz
09/10/2021, 12:32 PMMarko Novakovic
09/10/2021, 12:59 PMShakil Karim
09/10/2021, 1:03 PMMarko Novakovic
09/10/2021, 1:09 PMsubscribe
and dispose
type of scenario, flowWithLifecycle
provides that without boilerplateAdam Powell
09/10/2021, 1:41 PMIan Lake
09/10/2021, 2:42 PMshahroz
09/10/2021, 2:59 PMcollectAsState
but it still keep the previous destination compose.
I have tried to separate the view models for the both composable methods as well, but even then last destination keeps on collecting.
Tried flowWithLifecycle
as recommended by Marko and https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda
the composable still collects the state changes.Adam Powell
09/10/2021, 3:36 PMshahroz
09/10/2021, 6:19 PMshahroz
09/10/2021, 9:33 PMYou should only call navigate() as part of a callback and not as part of your composable itself, to avoid calling navigate() on every recomposition.
https://developer.android.com/jetpack/compose/navigation
But whats the solution for it? for example, i want to navigate on state change?
Example: You click on "Groups" tab, we fetch the groups from API, and if there is a group present you are a part of, I want to navigate the user to that particular group screen, otherwise just show the listings of the groups. This way, you will be on your group screen and if you navigate back, you are back to group listing screen.
Whats the best approach to handle such thing?Ian Lake
09/10/2021, 10:31 PMnavigate()
is a side-effect, so you should be using the APIs for handling side effects: https://developer.android.com/jetpack/compose/side-effects - i.e., LaunchedEffect(groupToLaunch) { navController.navigate("group/$groupToLaunch") }
Ian Lake
09/10/2021, 10:34 PMnavigate()
a second time. This would add the 'Groups' screen to the back stack, but run the right animation between whatever screen you're on directly to the group detail screenshahroz
09/10/2021, 11:07 PM