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.

--

--

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