Marco Righini
02/28/2022, 4:06 PMoverride fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.uiEvent
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.onEach { event ->
when (event) {
// code there
}
}.launchIn(lifecycleScope)
}
and we have a problem when we add the fragment to the back stack: we end up having coroutines when we press back and go back to this fragment (onViewCreated is called again)
The right approach seems to be to change lifecycleScope
with viewLifecycleOwner.lifecycleScope
.
Is it ok? If yes, could be worth to have a lint rule?
Thanks!Tower Guidev2
02/28/2022, 4:25 PMviewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.events
.onEach { event -> render(event) }
.flowOn(Dispatchers.Main)
.launchIn(this)
}
}
Marco Righini
02/28/2022, 4:55 PMobserve
Tower Guidev2
03/01/2022, 8:20 AMIan Lake
03/08/2022, 4:21 AM