0Pricing
Jetpack Compose Academy · บทเรียน

DisposableEffect และการล้างทรัพยากร

ลงทะเบียนและคืนทรัพยากรอย่างปลอดภัย

DisposableEffect และการล้างทรัพยากร เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 และการล้างทรัพยากร” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “DisposableEffect และการล้างทรัพยากร”

ลงทะเบียนและคืนทรัพยากรอย่างปลอดภัย คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “DisposableEffect และการล้างทรัพยากร” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม

ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. LaunchedEffect สำหรับโครูทีน
  2. rememberCoroutineScope สำหรับเหตุการณ์
  3. DisposableEffect และการล้างทรัพยากร
  4. derivedStateOf และ snapshotFlow
← กลับไปที่ Jetpack Compose Academy