The BEST android courses in the world: [ Ссылка ]
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: [ Ссылка ]
Follow me:
Instagram: [ Ссылка ]
Twitter: [ Ссылка ]
.
.
![](https://s2.save4k.ru/pic/pqM8EzFylnQ/maxresdefault.jpg)