Coroutine Basics and Scopes
Launch coroutines in the right scope.
Coroutine Basics and Scopes is a free Android Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.
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.
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Coroutine Basics and Scopes” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Coroutine Basics and Scopes”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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