We have this code in the codebase ``` override ...
# coroutines
m
We have this code in the codebase
Copy code
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!
t
what about this approach?
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.events
.onEach { event -> render(event) }
.flowOn(Dispatchers.Main)
.launchIn(this)
}
}
m
I think it’s the same as my proposed solution. The question is more about if it would be possible to have a lint rule as we had before with livedata
observe
t
lint rules are easy enough to create yourself or are you looking for a "standard" one supplied with Android Studio?
i
You can file an issue to add these to the existing set of Fragment lint checks: https://issuetracker.google.com/issues/new?component=460964&template=1182267