https://kotlinlang.org logo
#android
Title
# android
i

itnoles

06/06/2019, 5:14 AM
About Fragment implements CoroutineScope example, does cancel method needs on onDestroy()?
g

gildor

06/06/2019, 5:15 AM
sure
But I would recommend to always use
LifecycleOwner.lifecycleScope
and do not implement own scope for Fragments and Activities
g

gildor

06/06/2019, 5:17 AM
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

itnoles

06/06/2019, 5:18 AM
Do you have examples somewhere?
g

gildor

06/06/2019, 5:18 AM
of what?
i

itnoles

06/06/2019, 5:18 AM
Lifecycle and implementing own scope
g

gildor

06/06/2019, 6:39 AM
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)
13 Views