[Question] On GithubBrowserSample, how are ViewModel dependencies injected?
See original GitHub issueHi, Thanks for the very useful examples! They were really helpful!
I have two (probably basic) questions regarding Dagger and injection in ViewModels.
Sometimes we want to inject some dependencies in our class constructor. To do that, we just annotate our class constructor with @Inject and the dependencies passed to the constructor will be provided.
In the Architecture Components ViewModel, if we want to pass parameters to a ViewModel constructor, we have to use a Factory that knows how that ViewModel is instantiated. So I assume just annotating the ViewModel with @Inject wont’t work, since it is the Factory that is providing the ViewModel instance, not Dagger.
I saw that you do exactly that and use GithubViewModelFactory.java to provide instances of ViewModels.
However, I don’t understand two things in this class:
1 - How (T) creator.get() knows how to return a ViewModel instance with its dependencies injected on it?
2 - GithubViewModelFactory.java receives a Map<Class<? extends ViewModel>, Provider<ViewModel>> creators on its constructor. Where is this Map coming from?
Thanks! 😃
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:17 (1 by maintainers)

Top Related StackOverflow Question
But it does not work for me.I have defined ViewModelKey like below:
and ViewModelModule:
and AppComponent has include the module:
But when build the project, i got this in gradle console:
Is there someone i miss? @florina-muntenescu
Leaving a note in case anyone else has the same issue I did when following this sample using kotlin. If you get this error (same as https://github.com/googlesamples/android-architecture-components/issues/141#issuecomment-344869162)
java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,? extends javax.inject.Provider<android.arch.lifecycle.ViewModel>> cannot be provided without an @Provides-annotated method.You’ll need to add a @JvmSuppressWildcards annotation to the creator map’s value type param: