0Pricing
Jetpack Compose Academy · درس

النطاقات وأعمار المكونات

تحكم في مدة بقاء التبعيات

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

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

What a Scope Controls

A scope decides how long Hilt keeps a single instance alive and where it gets shared. It is about lifetime, not just creation.

Unscoped by Default

Without a scope annotation, Hilt builds a new instance every time it is requested. That is fine for cheap, stateless objects.

@Provides
fun provideMapper(): Mapper = Mapper() // fresh each time

Components Mirror Android

Hilt provides a set of components that match Android lifetimes, from the whole app down to a single view. Each owns its own instances.

App-Wide With Singleton

SingletonComponent lives as long as the app. Bindings here with @Singleton are created once and shared across every screen.

@InstallIn(SingletonComponent::class)

Per-Activity Lifetime

ActivityComponent follows one Activity. Use @ActivityScoped to share an instance across that Activity and recreate it on a new one.

@ActivityScoped
class FormState @Inject constructor()

ViewModel Scope

ViewModelComponent matches a ViewModel's life. With @ViewModelScoped a dependency is shared only within that one ViewModel.

@ViewModelScoped
class SessionCache @Inject constructor()

Scope Equals Annotation Plus Component

Each scope annotation pairs with one component. @Singleton goes with SingletonComponent, @ActivityScoped with ActivityComponent, and so on.

Lifetimes Form a Hierarchy

Components nest: an Activity lives inside the app, a Fragment inside the Activity. A child can use anything its parents provide.

Scope With Care

Scoping costs memory, because the instance is held until its component dies. Only scope objects whose state genuinely needs to be shared.

Avoid Leaking the Wider Scope

Never inject a short-lived object, like an Activity, into a Singleton. The longer-lived holder would keep it alive and leak memory. ⚠️

Picking the Right Scope

Match the scope to the data's life: app config in Singleton, screen state in ViewModelScoped. The right fit prevents leaks and waste.

Quick Check

You want one shared instance for the entire app lifetime. Which scope fits?

Recap: Lifetimes Under Control

You learned that scopes tie an instance's lifetime to a component, from Singleton down to ViewModel, so dependencies live exactly as long as needed. 🎯

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

هل درس «النطاقات وأعمار المكونات» مجاني؟

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

ماذا ستتعلم في «النطاقات وأعمار المكونات»؟

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

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

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

كم من الوقت يستغرق درس «النطاقات وأعمار المكونات»؟

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

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

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

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

  1. لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة
  2. الوحدات و@Provides و@Binds
  3. ‏hiltViewModel و@HiltViewModel
  4. النطاقات وأعمار المكونات
← العودة إلى Jetpack Compose Academy