Launching Coroutines in Android

Alex Queudot
L+R Engineers
Published in
5 min readMar 22, 2021

--

Photo by Fabio Bracht on Unsplash

Kotlin Coroutines have become the recommended approach to handle multi-threading and asynchronous programming on Android.
It is a lightweight solution written in Kotlin with support at the language level.

Coroutines are also compatible with Kotlin Multiplatform and can be used to share code between Android, iOS, Web, and Desktop.
Many Jetpack libraries already include extensions to take full advantage of coroutines.

This article focuses only on the creation, launch, and lifecycle of coroutines in Android.

If you are new to coroutines, check the resources below to learn more about them:

❌ The bad approach

When starting out, the easiest way to create a coroutine is using GlobalScope.launch { … }
This is very similar to creating a new Thread or AsyncTask and provides very little advantage over those.

Using GlobalScope as your coroutine scope creates it at the Application level and therefore will survive every lifecycle and configuration change. For example:

  • When the Activity is stopped, the coroutine will continue.

--

--