Learning Android Development
Understanding of Coroutine Scope Behavior for Fragment
Making the best of Coroutine Scope Behavior for the tasks
Fragment is one tricky things to handle in Android. Unlike Activity that only has start, stop, restore and configuration change state, Fragment has additional state like new Fragment got replaced, or added, which trigger different lifecycle events.
The crazy list of lifecycle variants as shown below would have blown one away if one doesn’t understand them thoroughly. Due to that it has many pit falls, as listed in 7 traps of using Fragment that we can easily falls into.
Now, with the new lifecycle aware coroutine scopes provided, how can we best manage it in Fragment?
To know the life of the coroutine scope, I have studied their behavior across
- The two variants of coroutine scope i.e.
lifecycleScope
andviewLifecyecleScope
- The launch API
whenCreated
,whenStarted
andwhenResumed
- The
repeatOnLifecycle
acrossCREATED
,STARTED
andRESUMED