override 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!