لماذا يصمد ViewModel أمام التدوير
حافظ على الحالة أثناء تغييرات الإعدادات
لماذا يصمد ViewModel أمام التدوير درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Rotation Problem
Rotate the screen and Android destroys your Activity, wiping any state you stored in it. Your counter, text, and scroll position all vanish. 😵
Configuration Changes
Rotation is one of many configuration changes: theme switches, locale changes, and window resizing all recreate your screen the same way.
Enter the ViewModel
A ViewModel is a class built to hold UI state that outlives the Activity it serves, so a rotation no longer throws your data away.
class CounterViewModel : ViewModel() {
var count = 0
}Tied to the Screen, Not the Activity
The ViewModel is scoped to the screen’s lifecycle owner, so the same instance is handed back to the recreated Activity instead of a fresh one.
How It Survives
Android keeps the ViewModel in a retained store across the configuration change, then reconnects it to your brand-new Activity automatically.
When It Is Cleared
A ViewModel lives until the screen is truly finished. When the user navigates away for good, onCleared() runs so you can release resources.
override fun onCleared() {
// cancel jobs, close streams
}Not for Everything
The ViewModel survives rotation but not process death. For that, pair it with SavedStateHandle so critical values can be restored later.
Add the Dependency
You get ViewModel support from the lifecycle libraries. Add the lifecycle-viewmodel-compose artifact so Compose can obtain one easily.
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0")Separation of Concerns
Beyond survival, a ViewModel keeps logic out of your UI. Your Composables stay focused on drawing while the ViewModel owns the state.
A Mental Model
Think of the ViewModel as a quiet backstage manager: the Activity comes and goes, but the manager keeps your show’s state ready to perform again.
Why This Matters
Surviving rotation means users never lose progress mid-task. That reliability is the foundation every robust Compose app is built on. ✨
Quick Check
Test your grasp of why a ViewModel outlives a rotation.
Recap
Rotation recreates your Activity, but a ViewModel survives configuration changes and reattaches, keeping UI state intact and logic out of the UI.
الأسئلة الشائعة
هل درس «لماذا يصمد ViewModel أمام التدوير» مجاني؟
نعم — نص درس «لماذا يصمد ViewModel أمام التدوير» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «لماذا يصمد ViewModel أمام التدوير»؟
حافظ على الحالة أثناء تغييرات الإعدادات تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «لماذا يصمد ViewModel أمام التدوير»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا يصمد ViewModel أمام التدوير
- viewModel() داخل Composable
- نمذجة فئة بيانات UiState
- collectAsStateWithLifecycle