hiltViewModel و@HiltViewModel
احقن ViewModels في Composables
hiltViewModel و@HiltViewModel درس مجاني في Jetpack Compose Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Jetpack Compose Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
ViewModels Need Dependencies Too
Your ViewModel usually depends on a repository or use case. Hilt can inject those for you, so you never build them by hand again.
Annotate the ViewModel
Mark the class with @HiltViewModel and add @Inject to its constructor. Hilt now knows how to create it with all its dependencies.
@HiltViewModel
class FeedViewModel @Inject constructor(private val repo: Repo) : ViewModel()Inject Anything It Needs
Constructor parameters can be any type Hilt knows, like a repository, a use case, or a logger. Hilt resolves the whole chain for you.
@HiltViewModel
class FeedViewModel @Inject constructor(
repo: Repo, logger: Logger
) : ViewModel()Get One in Compose
Inside a Composable, call hiltViewModel() to obtain a Hilt-built ViewModel. No factory boilerplate, just one clean call.
@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel()) { }Add the Compose Dependency
hiltViewModel() lives in the hilt-navigation-compose artifact. Add it to your Gradle file so the function is available in your screens.
implementation("androidx.hilt:hilt-navigation-compose:1.2.0")Default Parameter Pattern
Putting hiltViewModel() as a default parameter keeps the Composable testable. A test can still pass a fake ViewModel directly.
@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel())Scoped to the Owner
hiltViewModel() scopes the instance to the nearest ViewModelStoreOwner, usually the Activity or nav destination, so it survives recomposition.
One ViewModel Per Destination
Inside a NavHost, each destination gets its own ViewModel instance. Navigate away and back, and that screen receives a fresh one.
Mark the Activity Too
The host Activity must carry @AndroidEntryPoint so Hilt can deliver ViewModels into its Composables. Without it, injection fails.
@AndroidEntryPoint
class MainActivity : ComponentActivity()Expose State, Hide Logic
The injected ViewModel exposes UI state and the screen just reads it. Your Composable stays thin while logic lives in one tested place.
val state by vm.uiState.collectAsStateWithLifecycle()No Manual Factories
Before Hilt you wrote a ViewModelProvider.Factory for each ViewModel with arguments. Hilt removes that boilerplate completely. 🎁
Quick Check
Which pair correctly sets up a Hilt-injected ViewModel for a Composable?
Recap: Injected ViewModels
You learned to tag a ViewModel with @HiltViewModel and fetch it via hiltViewModel(), scoped to its owner with zero factory code. ✨
الأسئلة الشائعة
هل درس «hiltViewModel و@HiltViewModel» مجاني؟
نعم — نص درس «hiltViewModel و@HiltViewModel» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Jetpack Compose Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Jetpack Compose Academy 4 دروس في المجموع.
ماذا ستتعلم في «hiltViewModel و@HiltViewModel»؟
احقن ViewModels في Composables تتمرن على Jetpack Compose Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Jetpack Compose Academy؟
لا تُشترط خبرة سابقة. Jetpack Compose Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «hiltViewModel و@HiltViewModel»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Jetpack Compose Academy هذا؟
نعم. كل درس في Jetpack Compose Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا DI: تعليمات برمجية قابلة للاختبار ومفصولة
- الوحدات و@Provides و@Binds
- hiltViewModel و@HiltViewModel
- النطاقات وأعمار المكونات