Learning Android Development

Understanding of Coroutine Scope Behavior for Fragment

Making the best of Coroutine Scope Behavior for the tasks

Photo by Nathan Dumlao on Unsplash

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?

Variants of Fragment Lifecycle trigger. Check out Different lifecycle variants of Fragment for detail description

To know the life of the coroutine scope, I have studied their behavior across

  • The two variants of coroutine scope i.e. lifecycleScope and viewLifecyecleScope
  • The launch API whenCreated, whenStarted and whenResumed
  • The repeatOnLifecycle across CREATED, STARTED and RESUMED

--

--