collectAsStateWithLifecycle
راقب حالة ViewModel بأمان في Compose
collectAsStateWithLifecycle درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Observing a Flow in Compose
Your ViewModel exposes state as a Flow, and your Composable needs to turn that stream into a value Compose can read and react to.
The Lifecycle-Aware Way
Use collectAsStateWithLifecycle() to collect a StateFlow into Compose state while respecting the screen’s lifecycle. 🔋
val ui by vm.state.collectAsStateWithLifecycle()Why Not collectAsState
Plain collectAsState() keeps collecting even when the app is in the background, wasting work. The lifecycle version pauses instead.
Stops in the Background
When your screen stops, collection stops too; when it returns to the foreground, collection resumes, saving battery and avoiding pointless updates.
Reading the Result
The call returns a State you read with by, giving you a normal value that triggers recomposition whenever the flow emits.
val ui by vm.state.collectAsStateWithLifecycle()
Text(ui.title)Add the Dependency
This helper lives in a runtime artifact. Add lifecycle-runtime-compose so the function is available in your Composables.
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.0")Choosing the Min State
By default collection is active while at least STARTED. You can lower the threshold, but STARTED is the safe, recommended choice.
Pairs With StateFlow
It shines with StateFlow, which always holds a current value, so your screen has something to show the instant it appears.
Branch on the State
Once collected, branch on the UiState to render loading, success, or error, all driven by the latest emission from your ViewModel.
if (ui.isLoading) Spinner() else Content(ui.items)Safer Than Manual Collection
It frees you from manually starting and cancelling collection. The helper ties the work to composition and lifecycle for you. ✨
The Modern Default
For new Compose screens, collectAsStateWithLifecycle() is the recommended way to observe ViewModel state safely and efficiently.
Quick Check
Check why the lifecycle-aware collector is preferred.
Recap
Observe ViewModel state with collectAsStateWithLifecycle(): it collects a StateFlow into Compose state, pauses in the background, and is the modern default.
الأسئلة الشائعة
هل درس «collectAsStateWithLifecycle» مجاني؟
نعم — نص درس «collectAsStateWithLifecycle» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «collectAsStateWithLifecycle»؟
راقب حالة ViewModel بأمان في Compose تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «collectAsStateWithLifecycle»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا يصمد ViewModel أمام التدوير
- viewModel() داخل Composable
- نمذجة فئة بيانات UiState
- collectAsStateWithLifecycle