0Pricing
Jetpack Compose Academy · Lesson

DisposableEffect & Cleanup

Register and release resources safely.

DisposableEffect & Cleanup is a free Jetpack Compose Academy lesson on CoddyKit — lesson 3 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When You Must Clean Up

Some setup needs a matching teardown: registering a listener, opening a sensor, or observing the lifecycle. For that you use DisposableEffect.

Setup Plus onDispose

A DisposableEffect runs setup code and must end with an onDispose block. That block runs when the effect leaves or its key changes. 🧹

DisposableEffect(Unit) {
    sensor.start()
    onDispose { sensor.stop() }
}

The onDispose Is Required

Every DisposableEffect must return onDispose as its last statement. The compiler enforces this so you never forget to release resources.

Keys Work Like Before

Just like LaunchedEffect, a changed key tears down the old effect (running onDispose) and sets up a fresh one with the new value.

DisposableEffect(roomId) {
    chat.join(roomId)
    onDispose { chat.leave(roomId) }
}

Register and Unregister

The classic use is a callback or broadcast receiver: register it during setup and unregister it inside onDispose to avoid leaks.

DisposableEffect(Unit) {
    val cb = Listener()
    api.add(cb)
    onDispose { api.remove(cb) }
}

Observe the Lifecycle

You can add a LifecycleObserver on setup and remove it on dispose, which keeps lifecycle logic local to one Composable.

DisposableEffect(lifecycleOwner) {
    lifecycle.addObserver(obs)
    onDispose { lifecycle.removeObserver(obs) }
}

Why Not LaunchedEffect

LaunchedEffect cancels its coroutine but cannot run arbitrary cleanup. When you need a symmetric undo step, DisposableEffect is the tool.

No Suspend Functions Inside

A DisposableEffect block is not a coroutine, so you cannot call suspend functions directly here. Use it for plain setup and teardown.

Cleanup Runs Before Restart

If the key changes, onDispose runs before the new setup. Old resources are released first, so you never double-register.

Leaving the Screen Cleans Up

When the Composable leaves composition for good, onDispose fires one final time. That guarantee is what prevents resource leaks.

A Sensor Subscription Example

Start listening in setup, store the handle, and stop in onDispose. The effect mirrors the resource lifetime to the Composable.

DisposableEffect(Unit) {
    val sub = location.subscribe()
    onDispose { sub.cancel() }
}

Quick Check

Focus on what makes DisposableEffect unique.

Recap: DisposableEffect

Well done! DisposableEffect pairs setup with a required onDispose. It runs cleanup when the key changes or the Composable leaves, keeping resources leak-free. 🧹

Frequently asked questions

Is the “DisposableEffect & Cleanup” lesson free?

Yes — the full text of “DisposableEffect & Cleanup” is free to read here on the web, and the Jetpack Compose 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “DisposableEffect & Cleanup”?

Register and release resources safely. You practise Jetpack Compose 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 Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “DisposableEffect & Cleanup” 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 Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose 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

  1. LaunchedEffect for Coroutines
  2. rememberCoroutineScope for Events
  3. DisposableEffect & Cleanup
  4. derivedStateOf & snapshotFlow
← Back to Jetpack Compose Academy