Learning Android Development
Understand Android Lifecycle Aware Coroutine Scope Made Easy
Manage lifecycle more elegantly with Android provided Coroutine Scopes
To use Kotlin Coroutine, we need to have the appropriate scope defined. The tricky bit of it is, we have to remember to create, and stop them. In view of that, the Google Android development team has provided us with some Coroutine Scopes that are aware of its lifecycle.
Below, I’ll try to illustrate them below so that one can easily understand them.
In the examples, when I use
lifecycleScope
forActivity
, it isviewLifecycleOwner.lifecycleScope
forFragment
.
To learn specifically on Fragment check out
1. Repeat (restart) the Coroutine Scope Every Time on Specific Lifecycle
Sometimes we need to have some function executed whenever we are in a specific lifecycle and stop immediately when outside the lifecycle stage, e.g…