coroutine-recipes
coroutine-recipes copied to clipboard
Question
Hi, there is case regarding to ScopedFragment and "Kotlin android extensions" (old findViewById). In ScopedFragment, we cancel Job in onDestroy. AFAIK "Kotlin android extensions" assigns nulls to views in onDestroyView. So if there is coroutine running and trying to access a View after onDestroyView and before onDestroy, NullPointerException is thrown. This can be difficult to see beforehand, maybe we should add warning at Readme file here?
Hi,
Thank you for reporting this issue. Any suggestions for a fix?
I can't think of one clean solution, depending on use case, maybe:
- CoroutineScope can be created between onViewCreated()->onDestroyView() if coroutines change UI views
- CoroutineScope can be created between onCreate() -> onDestroy() if coroutines does not touch the UI
- Two CoroutineScopes in same Fragment for different purposes (mix of above two)
Another thing i stumbled upon is, there can be such flow in Fragment lifecycle:
onDestroyView()->onDetach()->onAttach()->onCreateView()onDestroy()andonCreate()is not called when fragment instance is alive, but view is destroyed and Fragment is detached.