Hilt ViewModels and Dependency Injection
Вставка
- Опубліковано 25 чер 2024
- The BEST android courses in the world: codingwithmitch.com/
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: github.com/mitchtabian/MVVMRe...
Follow me:
Instagram: / codingwithmitch
Twitter: / mitch_tabian
.
. - Наука та технологія
Deprecate @ViewModelInject. Equivalent functionality is now offered by @HiltViewModel, which is now part of the core Hilt Android APIs.
newer version of hilt use @HiltViewModel before the class and @Inject before the constructor
@HiltViewModel
class RecipeListViewModel @Inject constructor() : ViewModel() {
}
Also, AppModule's ApplicationComponent:class -> SingletonComponent:class
2:36 It seem in the new version of Hilt, we don't need `hilt-lifecycle-viewmodel` dependency
Appreciate the hilt info. Here's some complimentary engagement!
Many thanks Mitch. Very informative as usual. It makes things so much clearer when you show simple examples for each use case.
private val engagement : Engagement by Subscribers()
Excellent engagement
Thanks for nice video.
@HiltViewModel
class RecipeListViewModel
@Inject
constructor() : ViewModel() {
}
Thank you for the confirmation, it's confusing when all tutorial still in older version
Mitch thank you for your mention bro thats very cool!!!!!! ENGAGEMENT!
Hey Mitch, your each and every video is very informative and your explanation is too good
Great stuff 👍, looking forward to more hilt
Always so clear! thank you
Thank you so much for the amazing videos, mitch.
great work!!!!!! thank you for all your effort
Hey Mitch you're so powerful in teaching
thank you Mitch. Рахмат from Kyrgyzstan
Loving these lessons 👌
Nice one Mitch!
Many thanks to you Mitch 👍
Loved it 🙌🏻
Thanks Mitch, you saved my day 😍
Hi Engagements here!! :))
Thanks
Thank you Mitch ... from s.korea
Thank you, Mitch
your setup is really nice.
Thanks Mitch for the video
Great stuff 👍
Привет, Митч ! Отличное видео ! Кажется я понял, как использовать Hilt )
Thanks so much, this saved my code
Yes definitely wanna appreciate Google for hilt but equally wanna appreciate your efforts as well... Thanks ;)
Universal Android Boss
Thank you very helpful tutorial
I'm just in this part of the course and now i want the Kotlin mobile multiplatform course. btw take your engagement
thnx for the tutorial, appreciate it
Great content 🔥
love it Mitch! Engagement pls
Epic!
great stuff
cool as ever
Thanks Mitch
Hi Mitch!
Have you ever tried scrcpy instead of Vysor?
Take a look!
Tks for the coding class. I`m learning a lot here in Bazil.
Best regards
boom now I know hilt
BOOM
I like this!
Thanks!
Good master
Simplified learning experience, Here's some engagement
Hi Mitch, your videos are really very useful , would you pls create a multi module project using Hilt .. eagerly waiting for this video .. thanks in advance 🙂
nice one
Great work. Also, Can you cover inject viewModels with Fragment/Bundle arguments?
awesome
Good job! Do you consider to make video about "Error handling with coroutines and retrofit" ? Since there are a a lot of workarounds, i dont know how to use them efficiently.
Yeah that'll be in the next course. Generally I use something like this to execute the api/cache calls and handle errors github.com/mitchtabian/Clean-Notes/blob/master/app/src/main/java/com/codingwithmitch/cleannotes/business/data/util/RepositoryExtensions.kt
Thanks Mitch. My ViewModel was throwing an exception since I hadn't put
(kapt "androidx.hilt:hilt-compiler:1.0.0-alpha02") in my app's build.gradle.
Quick question, is field injection possible inside a ViewModel using Hilt?
I don't think so. but there's no reason to anyway. Hilt does all the heavy lifting so you can just magically pass anything as a constructor arg.
Ur the guy
remember bad old days view model injection with Dagger
That was great!! What about if you already have dagger? how can I implement Hilt while still have dagger. I don't want to do the migration just yet.
Engagement! Mitch try to use scrcpy instead of visor. You gonna love it!
I already paid for it so gotta get my monies worth 😂
thanks
What if you want to share view models using navigation scope or parent fragment scope only? how would you do that using this approach?
Can you record a tutorial on how to prepopulate Room db using Hilt?
I didn’t know Alexender Volkanovski knew so much about Android dev..
Hey Mitch, I was wondering if it might be error prone to use "by activityViewModels" when using Navigation Component with the Single Activity Pattern, as the View Model instance will then effectively be exposed to the entire scope of the app? I've heard of a way to scope View Models to individual navigation graphs with Hilt (haven't tried it yet), but do you know of a way where you can share a View Model instance between only a defined subset of Fragments? Thanks for the course, helps out a lot!
That is a very good question. I also have not tried the method of scoping to a graph. I don't know the answer to your question.
@@codingwithmitch I would love to see an answer to this question. Do you have any updates since then?
love his
What about sharing viewmodel between activities is it the same way ?
You should try scrcpy tool for screen mirroring,it's opensource and no need to install any app in your mobile so basically more secure
already paid for vysor so trying to get my monies worth
Engagementooooo :)
What if we want to inject something inside a Utility class. Can we still use @AndroidEntryPoint? In koin di framework we use KoinComponent everywhere. Please answer.
Hi Mitch. Is there any convenient solution to pass parameters to ViewModel in runtime by Hilt?
look into Assisted Inject
Hi Mitch, please can you make more videos about CRUD MVI using Kotlin?
Entire course right here my friend codingwithmitch.com/courses/android-clean-architecture/
I get an error on your website when trying to register. I would like to follow the Hilt course!
Hi when I created fragments its ask to implement these two methods inside fragment class. I havent any idea about this
override fun prepareCall(
contract: ActivityResultContract,
callback: ActivityResultCallback
): ActivityResultLauncher {
TODO("Not yet implemented")
}
override fun prepareCall(
contract: ActivityResultContract,
registry: ActivityResultRegistry,
callback: ActivityResultCallback
): ActivityResultLauncher {
TODO("Not yet implemented")
}
What if there are two different string dependency in the hilt module and we want both of them , then how would dagger inject both ???
watch next video
Does someone receive the following error when starting the application: Error inflating class androidx.fragment.app.FragmentContainerView
Hello
I don't understand this
by viewModel()
It means we don't have to initialise like this
ViewModelProvider(this).get(MyViewModel::class.java) ??
engagement
Engagementtttttttt
Why can't i annotate my activities&fragments with AndroidEntryPoint when the classes are abstract ? like an abstract baseActivity !! is there anyway to handle this ?
Actually I haven't tried. Didn't know that
Why don't you switch to dark theme ? I think that would make viewers happy, and your eyes :D
I find it difficult to ENGAGE with Android Studio when Dagger/Hilt is being used in a project. Looks like Modern Android Development with Hilt involed needs to have separate system requirements Column on Android Studio page 😂
Lol just watch my hilt course it's free
Yeah I've already checked that and have tried to use Hilt in project. Hilt makes DI incredibly easy but since it under the hood uses Dagger only which heavily adds to your build time, I decided not to use Hilt unless I get an access to high specs machine. I feel atleast 16GB of RAM with SSD is must for using these tools smoothly.
Mitch please help. I am stuck with the DAO video Data Entity thing. Please suggest where to read about it. You are moving too fast.
We don't even have a Dao
@@codingwithmitch ua-cam.com/video/rTWCxow5IJQ/v-deo.html Forgive me, its this one.
@@avinashdas1013 That is a DTO not a DAO. Dao is used with room persistence library.
@@codingwithmitch Thank you Mitch. I have never heard these terminology before. Sure I have to do alot of catch-up before I can continue with this series. 🙏🙏
Engagement
You always forget to add card. Btw engagement is new normal 😂.
How we can Create BaseFragment to instantiate ViewModel- val viewModel: EngagementViewModel by viewmodels() - thanks! -
I don't think you need to do anything special. Try instantiating it in the abstract class I think it will work fine. I could be wrong tho I'm not at my computer
Hilt wont import the ApplicationComponent class
It's SingletonComponent now SAM YOU SHOULD KNOW THAT
@@codingwithmitch lol i figured. Damn I'm way behind 😂
@@codingwithmitch but it did actually worked yesterday and this morning it just wouldn't work. i had to open the documents and it was right there 🤣
Mitch
Jitender
You can rest assured that I will never watch anything else than a free course.
some engagement 4 u
I think the ViewModelnject is deprecated now and the right one is ViewModelScoped :)
use HiltViewModel
That dependency doesn't exist anymore on that page
bump
Like for shared VM trick
you obviously know the content and are very excellent with android, but its difficult for me to follow because you talk way too fast and just "code". more of "how its done kinda guy," not "what it is and why its doing this". because of that i dont understand the code and your speech speeed doesnt help. kudos all the same @least for those who seem to understand you
Engagement