Jetpack Compose Academy · درس

‏LaunchedEffect للتدفقات التعاونية

ابدأ عملًا معلّقًا مرتبطًا بالتركيب

الدرس 1 من 413 خطوة

‏LaunchedEffect للتدفقات التعاونية درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Effects Live Outside Composition

Composables should be pure and run many times. When you need to do real work like a network call, you reach for a side effect handler instead.

Meet LaunchedEffect

A LaunchedEffect runs suspend code in a coroutine that is tied to where it sits in your composition. Perfect for one-shot startup work. 🚀

It Starts When It Enters

When a LaunchedEffect first enters the composition, it launches its block. When it leaves the composition, the coroutine is cancelled for you.

LaunchedEffect(Unit) {
    loadProfile()
}

The Key Controls Restarts

The first argument is a key. If the key changes between recompositions, the old coroutine is cancelled and a fresh one starts.

LaunchedEffect(userId) {
    user = fetchUser(userId)
}

Use Unit to Run Once

Passing Unit (or true) as the key means the effect never restarts. It runs a single time when the Composable appears.

LaunchedEffect(Unit) {
    analytics.logScreenView()
}

Suspend Calls Are Welcome

Inside the block you can freely call suspend functions like delay or a Retrofit request. The coroutine scope is provided for you.

LaunchedEffect(Unit) {
    delay(2000)
    showSplash = false
}

Never Launch in the Body

Calling a coroutine directly in the Composable body is a bug. The body re-runs on every recomposition, so you would fire the work again and again.

Cancellation Is Automatic

When the user navigates away and the Composable leaves the screen, Compose cancels the coroutine. No manual cleanup, no leaks.

Pair It with State

Effects usually write their result into state so the UI redraws. Store the loaded data, and recomposition shows it.

var data by remember { mutableStateOf<List<Item>>(emptyList()) }
LaunchedEffect(Unit) { data = repo.load() }

Multiple Keys for Combined Triggers

You can pass several keys. The effect restarts whenever any of them changes, which is handy for re-fetching on filter changes.

LaunchedEffect(query, sort) {
    results = search(query, sort)
}

A Typical Loading Pattern

Together it reads like a recipe: set loading true, await the result, then store it. Compose handles the lifecycle around your coroutine.

LaunchedEffect(id) {
    loading = true
    item = repo.get(id)
    loading = false
}

Quick Check

Think about how restarts are controlled.

Recap: LaunchedEffect

Nice work! LaunchedEffect runs suspend code tied to composition, restarts when its key changes, and cancels itself automatically when the Composable leaves. 🎯

البدء مجانًا

تعلم Kotlin مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «‏LaunchedEffect للتدفقات التعاونية» مجاني؟

نعم — نص درس «‏LaunchedEffect للتدفقات التعاونية» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.

ماذا ستتعلم في «‏LaunchedEffect للتدفقات التعاونية»؟

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

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

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

كم من الوقت يستغرق درس «‏LaunchedEffect للتدفقات التعاونية»؟

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

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

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

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

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