DisposableEffectとクリーンアップ
リソースを安全に登録し、解放します。
「DisposableEffectとクリーンアップ」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 🧹
よくある質問
「DisposableEffectとクリーンアップ」レッスンは無料ですか?
はい。「DisposableEffectとクリーンアップ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「DisposableEffectとクリーンアップ」で何を学びますか?
リソースを安全に登録し、解放します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「DisposableEffectとクリーンアップ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- コルーチン用のLaunchedEffect
- イベント用のrememberCoroutineScope
- DisposableEffectとクリーンアップ
- derivedStateOfとsnapshotFlow