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
    .
    .
  • Наука та технологія

КОМЕНТАРІ • 118

  • @burakkarabekir2863
    @burakkarabekir2863 3 роки тому +17

    Deprecate @ViewModelInject. Equivalent functionality is now offered by @HiltViewModel, which is now part of the core Hilt Android APIs.

  • @tarikhusin1767
    @tarikhusin1767 2 роки тому +20

    newer version of hilt use @HiltViewModel before the class and @Inject before the constructor
    @HiltViewModel
    class RecipeListViewModel @Inject constructor() : ViewModel() {
    }

    • @typingcat
      @typingcat 2 роки тому +1

      Also, AppModule's ApplicationComponent:class -> SingletonComponent:class

  • @NMNaufaldo
    @NMNaufaldo 3 роки тому +27

    2:36 It seem in the new version of Hilt, we don't need `hilt-lifecycle-viewmodel` dependency

  • @jswaby
    @jswaby 3 роки тому +5

    Appreciate the hilt info. Here's some complimentary engagement!

  • @staceyrichards8656
    @staceyrichards8656 3 роки тому

    Many thanks Mitch. Very informative as usual. It makes things so much clearer when you show simple examples for each use case.

  • @mikemikee7797
    @mikemikee7797 3 роки тому +9

    private val engagement : Engagement by Subscribers()

  • @dijitaldurum7160
    @dijitaldurum7160 3 роки тому +3

    Thanks for nice video.
    @HiltViewModel
    class RecipeListViewModel
    @Inject
    constructor() : ViewModel() {
    }

    • @vincentlius1569
      @vincentlius1569 2 роки тому

      Thank you for the confirmation, it's confusing when all tutorial still in older version

  • @felipefranco7444
    @felipefranco7444 3 роки тому +2

    Mitch thank you for your mention bro thats very cool!!!!!! ENGAGEMENT!

  • @amleshkumar4917
    @amleshkumar4917 Рік тому +1

    Hey Mitch, your each and every video is very informative and your explanation is too good

  • @judefrancis
    @judefrancis 3 роки тому +1

    Great stuff 👍, looking forward to more hilt

  • @MedAmineSouaiaia88
    @MedAmineSouaiaia88 3 роки тому +2

    Always so clear! thank you

  • @muhammedshahin6299
    @muhammedshahin6299 Рік тому

    Thank you so much for the amazing videos, mitch.

  • @deepakgahlot6879
    @deepakgahlot6879 3 роки тому +1

    great work!!!!!! thank you for all your effort

  • @mohsenmdb9786
    @mohsenmdb9786 Рік тому

    Hey Mitch you're so powerful in teaching

  • @nurseyittursunkulov3319
    @nurseyittursunkulov3319 3 роки тому

    thank you Mitch. Рахмат from Kyrgyzstan

  • @hellespont5072
    @hellespont5072 3 роки тому

    Loving these lessons 👌

  • @davidscammell9823
    @davidscammell9823 3 роки тому +1

    Nice one Mitch!

  • @moussawade5957
    @moussawade5957 3 роки тому +1

    Many thanks to you Mitch 👍

  • @gunjotsingh3638
    @gunjotsingh3638 2 роки тому

    Loved it 🙌🏻

  • @vichuz100
    @vichuz100 Рік тому

    Thanks Mitch, you saved my day 😍

  • @SatyamRaikwar262238
    @SatyamRaikwar262238 2 роки тому

    Hi Engagements here!! :))
    Thanks

  • @user-lf9lx2gn8n
    @user-lf9lx2gn8n 2 роки тому

    Thank you Mitch ... from s.korea

  • @ANIKINKIRILL
    @ANIKINKIRILL 3 роки тому +1

    Thank you, Mitch

  • @brijeshmorya9737
    @brijeshmorya9737 Рік тому

    your setup is really nice.

  • @yogenp5763
    @yogenp5763 3 роки тому

    Thanks Mitch for the video

  • @watziznaim
    @watziznaim 4 місяці тому

    Great stuff 👍

  • @mishaeliseev
    @mishaeliseev 3 роки тому

    Привет, Митч ! Отличное видео ! Кажется я понял, как использовать Hilt )

  • @leolizanon2503
    @leolizanon2503 Рік тому

    Thanks so much, this saved my code

  • @ashurajput11
    @ashurajput11 3 роки тому

    Yes definitely wanna appreciate Google for hilt but equally wanna appreciate your efforts as well... Thanks ;)

  • @MuhammadNaveed2016
    @MuhammadNaveed2016 3 роки тому +1

    Universal Android Boss

  • @blackballization
    @blackballization 2 роки тому

    Thank you very helpful tutorial

  • @oscarivan7697
    @oscarivan7697 3 роки тому

    I'm just in this part of the course and now i want the Kotlin mobile multiplatform course. btw take your engagement

  • @jvoyager1
    @jvoyager1 3 роки тому

    thnx for the tutorial, appreciate it

  • @be_like__prateek
    @be_like__prateek 3 роки тому

    Great content 🔥

  • @eiphyuphwe9269
    @eiphyuphwe9269 3 роки тому

    love it Mitch! Engagement pls

  • @datadata4039
    @datadata4039 3 роки тому

    Epic!

  • @tonnie7079
    @tonnie7079 2 роки тому

    great stuff

  • @arulwastaken
    @arulwastaken 3 роки тому

    cool as ever

  • @MrMayur6028
    @MrMayur6028 3 роки тому

    Thanks Mitch

  • @faustofl
    @faustofl 3 роки тому +1

    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

  • @martindelbel
    @martindelbel 3 роки тому +2

    boom now I know hilt

  • @johnybaby9574
    @johnybaby9574 3 роки тому

    I like this!

  • @vanosuperhero
    @vanosuperhero 2 роки тому

    Thanks!

  • @wajdidaghlas3178
    @wajdidaghlas3178 3 роки тому +1

    Good master

  • @amansahai7502
    @amansahai7502 3 роки тому

    Simplified learning experience, Here's some engagement

  • @subhendubanerjee8600
    @subhendubanerjee8600 3 роки тому

    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 🙂

  • @terencejumba1939
    @terencejumba1939 2 роки тому

    nice one

  • @devguru8443
    @devguru8443 3 роки тому

    Great work. Also, Can you cover inject viewModels with Fragment/Bundle arguments?

  • @LEGENDMORTAL
    @LEGENDMORTAL 2 роки тому

    awesome

  • @berkaykirecci1169
    @berkaykirecci1169 3 роки тому +1

    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.

    • @codingwithmitch
      @codingwithmitch  3 роки тому +2

      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

  • @tomandjerryofficial2030
    @tomandjerryofficial2030 3 роки тому

    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?

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      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.

  • @leetcodeforbeginners
    @leetcodeforbeginners 3 роки тому +1

    Ur the guy

  • @hnim2292
    @hnim2292 3 роки тому +1

    remember bad old days view model injection with Dagger

  • @genesiscatano
    @genesiscatano 2 роки тому

    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.

  • @YouGuto
    @YouGuto 3 роки тому

    Engagement! Mitch try to use scrcpy instead of visor. You gonna love it!

    • @codingwithmitch
      @codingwithmitch  3 роки тому +1

      I already paid for it so gotta get my monies worth 😂

  • @saranshsinghal132
    @saranshsinghal132 10 місяців тому

    thanks

  • @nayeramohamed436
    @nayeramohamed436 3 роки тому

    What if you want to share view models using navigation scope or parent fragment scope only? how would you do that using this approach?

  • @kamilboryczka7117
    @kamilboryczka7117 3 роки тому

    Can you record a tutorial on how to prepopulate Room db using Hilt?

  • @freesk13r
    @freesk13r 3 роки тому

    I didn’t know Alexender Volkanovski knew so much about Android dev..

  • @emil_fa
    @emil_fa 3 роки тому +1

    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!

    • @codingwithmitch
      @codingwithmitch  3 роки тому +1

      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.

    • @i11uminati87
      @i11uminati87 3 роки тому

      @@codingwithmitch I would love to see an answer to this question. Do you have any updates since then?

  • @applombaa6278
    @applombaa6278 2 роки тому

    love his

  • @Zaki-Was-Here
    @Zaki-Was-Here 3 роки тому

    What about sharing viewmodel between activities is it the same way ?

  • @vineedcool
    @vineedcool 3 роки тому

    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

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      already paid for vysor so trying to get my monies worth

  • @anuradhaamarasinghe9649
    @anuradhaamarasinghe9649 2 роки тому

    Engagementooooo :)

  • @vengateshm2122
    @vengateshm2122 3 роки тому

    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.

  • @sirlordpouya
    @sirlordpouya 3 роки тому

    Hi Mitch. Is there any convenient solution to pass parameters to ViewModel in runtime by Hilt?

  • @juniorlapunete4660
    @juniorlapunete4660 3 роки тому

    Hi Mitch, please can you make more videos about CRUD MVI using Kotlin?

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      Entire course right here my friend codingwithmitch.com/courses/android-clean-architecture/

  • @Rafael-hk9pg
    @Rafael-hk9pg 2 роки тому

    I get an error on your website when trying to register. I would like to follow the Hilt course!

  • @sanushradalage4091
    @sanushradalage4091 3 роки тому

    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")
    }

  • @vaibhavjaiswal5911
    @vaibhavjaiswal5911 3 роки тому

    What if there are two different string dependency in the hilt module and we want both of them , then how would dagger inject both ???

  • @jordan9506
    @jordan9506 3 роки тому

    Does someone receive the following error when starting the application: Error inflating class androidx.fragment.app.FragmentContainerView

  • @naimishmodi4193
    @naimishmodi4193 3 роки тому

    Hello
    I don't understand this
    by viewModel()
    It means we don't have to initialise like this
    ViewModelProvider(this).get(MyViewModel::class.java) ??

  • @prashantsurti5788
    @prashantsurti5788 3 роки тому +2

    engagement

  • @DS-bz4mz
    @DS-bz4mz Рік тому

    Engagementtttttttt

  • @khiariyoussef6674
    @khiariyoussef6674 3 роки тому

    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 ?

  • @slaviccat5375
    @slaviccat5375 3 роки тому

    Why don't you switch to dark theme ? I think that would make viewers happy, and your eyes :D

  • @kshitijpatil2019
    @kshitijpatil2019 3 роки тому +1

    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 😂

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      Lol just watch my hilt course it's free

    • @kshitijpatil2019
      @kshitijpatil2019 3 роки тому

      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.

  • @avinashdas1013
    @avinashdas1013 3 роки тому

    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.

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      We don't even have a Dao

    • @avinashdas1013
      @avinashdas1013 3 роки тому

      @@codingwithmitch ua-cam.com/video/rTWCxow5IJQ/v-deo.html Forgive me, its this one.

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      @@avinashdas1013 That is a DTO not a DAO. Dao is used with room persistence library.

    • @avinashdas1013
      @avinashdas1013 3 роки тому +1

      @@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. 🙏🙏

  • @ChrisAthanas
    @ChrisAthanas 3 роки тому +1

    Engagement

  • @vedx
    @vedx 3 роки тому +3

    You always forget to add card. Btw engagement is new normal 😂.

  • @iam-learning6374
    @iam-learning6374 3 роки тому

    How we can Create BaseFragment to instantiate ViewModel- val viewModel: EngagementViewModel by viewmodels() - thanks! -

    • @codingwithmitch
      @codingwithmitch  3 роки тому

      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

  • @samha1513
    @samha1513 3 роки тому +1

    Hilt wont import the ApplicationComponent class

    • @codingwithmitch
      @codingwithmitch  3 роки тому +1

      It's SingletonComponent now SAM YOU SHOULD KNOW THAT

    • @samha1513
      @samha1513 3 роки тому +1

      @@codingwithmitch lol i figured. Damn I'm way behind 😂

    • @samha1513
      @samha1513 3 роки тому +1

      @@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 🤣

  • @JitubhaiyaPhysics
    @JitubhaiyaPhysics 3 роки тому +1

    Mitch

  • @johnchance5446
    @johnchance5446 2 роки тому

    You can rest assured that I will never watch anything else than a free course.

  • @farjadilyas9549
    @farjadilyas9549 2 роки тому

    some engagement 4 u

  • @jordan9506
    @jordan9506 3 роки тому

    I think the ViewModelnject is deprecated now and the right one is ViewModelScoped :)

  • @peshutanpavri1599
    @peshutanpavri1599 2 роки тому

    That dependency doesn't exist anymore on that page

  • @alishechka29
    @alishechka29 3 роки тому

    bump

  • @alexanderk9767
    @alexanderk9767 3 роки тому

    Like for shared VM trick

  • @michaeli5113
    @michaeli5113 3 роки тому

    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

  • @muhammadshayankhan3066
    @muhammadshayankhan3066 3 роки тому +1

    Engagement