Dagger: ContributesAndroidInjector support for androidx.lifecycle.ViewModel

Created on 3 Sep 2018  路  10Comments  路  Source: google/dagger

is there any plan for support androidx.lifecycle.ViewModel with @ContributesAndroidInjector?

Most helpful comment

We need to think through a few things, but the short answer is yes.

All 10 comments

Also wondering this same thing

+1

We need to think through a few things, but the short answer is yes.

Is there a timeline on that?

Would be really useful. Already got burned by this when implemented such injection myself. It turns out, depending on how/when you inject Fragment (in my case), dagger may "help" retain a "cleared" ViewModel instance. A quick fix in my case was to use Provider<> to get a just-in-time "alive" instance of my ViewModel.

It would also be extremely convenient if dagger could generate ViewModel Factories. From what I understand, the thing is a necessary (evil) piece of boilerplate code required by the framework. It has no other development value. Dagger could derive Factory implementation from injectable ViewModel constructor.

Dagger could derive Factory implementation from injectable ViewModel constructor.

See my comment here, you can create that factory easily yourself.

I'm closing this since with the release of Hilt and the Jetpack extension for ViewModels using Hilt, I think this feature request for dagger.android is unlikely to be done.

This is what @Chang-Eric is referring to: https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodels

Sadly I wish there was a "vanilla" Dagger (aka w/out dagger-android or Hilt) way of doing this.

Vanilla Dagger is platform agnostic. That's why it is vanilla 馃槈

Yeah I was complaining about ViewModel implementation, not Dagger =) It's a bummer they didn't give us constructor access.

Was this page helpful?
0 / 5 - 0 ratings