About Fragment implements CoroutineScope example, ...
# android
i
About Fragment implements CoroutineScope example, does cancel method needs on onDestroy()?
g
sure
But I would recommend to always use
LifecycleOwner.lifecycleScope
and do not implement own scope for Fragments and Activities
g
One more problem with Fragment and CoroutineScope, that if you have any coroutine that bound to View lifecycle rather than to Fragment lifecycle it’s too easy accidentally to use
launch
of Fragment scope
Probably make sense to remove this example, because it confuses Android users and encurage to implement own scopes
again, Fragment is not different from Activity in general, you have to cancel scope onDestroy
i
Do you have examples somewhere?
g
of what?
i
Lifecycle and implementing own scope
g
Implementing own scope is just like in doc which you shared above
For lifecycleScope see Android libs documentation https://developer.android.com/topic/libraries/architecture/coroutines
👍 2
But I recommend to use lifecycleScope, it’s safer (no chance that you forgot to cancel scope, explicit separation of Fragment and View lifecycles), less code, more flexible for fragments (no need to create and manage view scope)