coroutine-recipes icon indicating copy to clipboard operation
coroutine-recipes copied to clipboard

Question

Open jemshit opened this issue 6 years ago • 2 comments

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?

jemshit avatar Jan 24 '19 14:01 jemshit

Hi,

Thank you for reporting this issue. Any suggestions for a fix?

dmytrodanylyk avatar Jan 25 '19 04:01 dmytrodanylyk

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() and onCreate() is not called when fragment instance is alive, but view is destroyed and Fragment is detached.

jemshit avatar Jan 25 '19 09:01 jemshit