0Pricing
Jetpack Compose Academy · درس

‏DisposableEffect والتنظيف

سجّل الموارد وحرّرها بأمان

‏DisposableEffect والتنظيف درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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 والتنظيف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

ماذا ستتعلم في «‏DisposableEffect والتنظيف»؟

سجّل الموارد وحرّرها بأمان تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟

لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «‏DisposableEffect والتنظيف»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟

نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. ‏LaunchedEffect للتدفقات التعاونية
  2. ‏rememberCoroutineScope للأحداث
  3. ‏DisposableEffect والتنظيف
  4. ‏derivedStateOf وsnapshotFlow
← العودة إلى Jetpack Compose Academy