In this video I show you how to do dependency injection into viewmodels using Hilt. In the previous video I showed you how to do field injection into an activity. Constructor injection is preferable over field injection whenever possible because then you know exactly what a class needs to be built.

Hilt makes it very easy to instantiate viewmodels in Fragments and activities. You can use the “by viewmodels()” method to maintain a viewmodel instance in a fragment. Or use the “by activityViewModels()” method to maintain a viewmodel instance between multiple fragments sharing the same host activity.

Code: https://github.com/mitchtabian/MVVMRecipeApp/tree/hilt-viewmodels

Subscribe : https://www.youtube.com/channel/UCoNZZLhPuuRteu02rh7bzsw

#web-development #hilt

Hilt ViewModels and Dependency Injection
2.15 GEEK