You're reading for free via Android Dev Nexus' Friend Link. Become a member to access the best of Medium.

Member-only story

Coroutines Cheat Sheet for Android Developers

Android Dev Nexus
4 min readOct 4, 2024

Hola, you lovely devs! 👩🏻‍💻

Welcome to yet another blog of our ongoing Android Interview Prep series. In this blog, we’ll be discussing about a really important concept in Android: Kotlin Coroutines. This coroutine cheat sheet serves as a quick refresher for anyone familiar with this powerful tool.

And here’s a special treat for our non-Medium members: we’re offering free access to this blog! Just follow this link — because your curiosity and passion for coding should never be limited! 😉😉

Let’s get the party started…

  1. Launching a Coroutine (launch)
  • launch is used to start a coroutine without expecting a result.
  • It’s usually used when you just want to fire and forget a task.

Example Usage:

2. Getting a Result with async

  • If you need a result from a coroutine, async is your friend.
  • async returns a Deferred object, which you can await to get the result.

🐼️Deferred is a type of coroutine job that holds a result, and you can retrieve this result asynchronously.

🐼 To get the result, you use await(), which suspends (pauses) the coroutine until the result is available.

⭐️ Bonus ⭐ ️

A coroutine job represent the lifecycle of a single coroutine. It controls the coroutine’s execution (starting a coroutine), cancelling a coroutine or checking its status i.e. if it’s active or not. Every coroutine has a Job object, which you can use to manage it.

In this example, the job controls the coroutine. After 500ms, the job is canceled, and the coroutine doesn't complete.

3. Coroutine Scopes

A CoroutineScope manages the lifecycle of one or more coroutines.

Before moving ahead, let’s understand what this means —

🐼 In this example, we use viewModelScope to launch two coroutines.

🐼 When the ViewModel is cleared (e.g. when the user navigates away from the screen), both coroutines are automatically canceled. Why? Because these coroutines are tied to the ViewModel lifecycle. (because of viewModelScope)

Let’s continue with our topic…

Here are the scopes that we need to know about : GlobalScope, lifecycleScope, and viewModelScope

  • GlobalScope: Runs for the whole app lifecycle.
  • lifecycleScope: Tied to Android lifecycle components like Activity or Fragment.
  • viewModelScope: Scoped to a ViewModel, automatically canceled when the ViewModel is cleared.

🐼 Coroutine Scopes are commonly used to group coroutines that should be tied to a specific lifecycle, like an Activity or ViewModel in Android.

Example usage: In Android, lifecycleScope or viewModelScope ensures that coroutines are canceled when the Activity or ViewModel is destroyed.

4. Dispatchers

Dispatchers control the thread on which the coroutine runs:

  • Dispatchers.Main: Runs on the main (UI) thread.
  • Dispatchers.IO: Runs on a background thread for I/O tasks like reading files or making network requests.
  • Dispatchers.Default: For CPU-intense tasks like data processing, sorting or complex calculations.
  • Dispatchers.Unconfined: Starts in the current thread but can move to other threads. Say, the coroutine is started in UI thread and it can switch to background thread after reaching a suspension point likedelay or withContext .

🐼 The “current thread” refers to the thread where the coroutine is launched or started. It could be the main thread or the background thread (i.e. any thread where coroutine is started or launched)

Example usage:

Starting on thread: main
Resuming on thread: DefaultDispatcher-worker-1 (background thread)

Wrapping Up: What’s Next?

By now, you’ve learned the basics of Kotlin coroutines, from launching coroutines to handling dispatchers. But this is just the beginning. In Part 2, we’ll dive deeper into advanced topics like:

  • Structured concurrency: How to keep coroutines organized and avoid leaks.
  • Exception handling in coroutines: How to manage errors across multiple coroutines.
  • Flow: An introduction to asynchronous data streams, a powerful tool for handling large amounts of data efficiently.

Stay tuned! We’ll explore these concepts and more, taking your coroutine knowledge to the next level with our cheatsheet.

Got any topic requests? We’re all ears! 👂 Feel free to send us topic requests at droid.dev.nexus@gmail.com or hit us up in the comments — we post new blogs daily! 🧑🏻‍💻 ⭐ ️

If you found this helpful, don’t forget to clap for the blog or or buy us a coffee here ☕️! Follow for more such posts.
Until then, happy coding!

٩(^ᗜ^ )و

Android Dev Nexus
Android Dev Nexus

Written by Android Dev Nexus

Your friendly neighborhood developers working at PayPal, turning Android fundamentals into concepts you’ll actually understand.

Responses (1)

Write a response

The perfect cheat sheet for Cororutines🤩