Coroutine Basics and Scopes
Launch coroutines in the right scope.
Coroutine Basics and Scopes is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are Coroutines?
Coroutines let you write asynchronous, non-blocking code in a sequential style. They are Kotlin’s answer to callbacks and threads for tasks like network and database I/O.
suspend Functions
A function marked suspend can pause without blocking the thread and resume later. It can only be called from a coroutine or another suspend function.
suspend fun fetchUser(): User {
delay(1000) // non-blocking pause
return User(1, "Ada", "ada@x.com")
}Suspending vs Blocking
delay suspends the coroutine but frees the thread for other work. Thread.sleep blocks the whole thread. Coroutines favor suspension to stay efficient.
Launching a Coroutine
Start a coroutine with launch on a CoroutineScope. It runs concurrently and does not return a result.
scope.launch {
val user = fetchUser()
println(user.name)
}Getting a Result with async
Use async when you need a value back. It returns a Deferred you await.
val deferred = scope.async { fetchUser() }
val user = deferred.await()What Is a CoroutineScope?
A scope defines the lifetime of the coroutines launched in it. Cancel the scope and all its coroutines are cancelled. This is structured concurrency.
Structured Concurrency
Because coroutines live inside a scope, they cannot outlive it accidentally. This prevents leaks: when the screen goes away, its scope cancels, and its work stops.
Cancellation
Cancelling a scope or job throws a CancellationException at the next suspension point, cleanly stopping the coroutine.
val job = scope.launch { /* work */ }
job.cancel() // stops it cooperativelyCooperative Cancellation
Coroutines must cooperate: long CPU loops should check isActive or call a suspend function so cancellation can take effect.
while (isActive) {
// do a chunk of work
}Exceptions in Coroutines
An uncaught exception in a launch propagates to the scope and cancels its siblings (unless using a SupervisorJob). Handle errors with try/catch or a CoroutineExceptionHandler.
Why Scopes Matter in Android
Android provides ready-made scopes tied to component lifecycles — like viewModelScope and lifecycleScope — so your coroutines are automatically cancelled at the right time. That is the next lesson.
Quick Check
Test your understanding of coroutine scopes.
Recap
You learned:
suspendfunctions pause without blocking.launchstarts fire-and-forget work;asyncreturns a result.- A CoroutineScope bounds coroutine lifetimes (structured concurrency).
- Cancellation is cooperative.
Frequently asked questions
Is the “Coroutine Basics and Scopes” lesson free?
Yes — the full text of “Coroutine Basics and Scopes” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
What will I learn in “Coroutine Basics and Scopes”?
Launch coroutines in the right scope. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Coroutine Basics and Scopes” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Coroutine Basics and Scopes
- viewModelScope and Dispatchers
- Kotlin Flow Basics
- StateFlow and SharedFlow