Throughout our project, we used the @Inject annotation to declare classes that can be injected. In this lesson, we're going to be configuring the dependency injection for our project so that these classes can be provided.
Why do we need a custom ViewModelFactory? Why does the default one doesn't work for our specific project?
@ContributesAndroidInjector works for injecting Activities and is coupled with the AndroidInjector module we added previously, right? But where are we going to inject this activities? Is there really a need for this?
We need to use a custom view model factory class because we are injecting our view model instances into our classes. We could use the ViewModelFactory that is provided by architecture components, but we would need to define a ViewModelModule (similar to how we do for other parts of the app), and having to update this for every ViewModel could get a tedious, our custom factory gives us the ability to be flexible with our Injection for View Models.
The @ContributesAndroidInjector is required for components to be injected into the activities, as in the places where we declare AndroidInjection.inject() :)
Hi Joe, first of all thanks a lot for the tutorial. Although not easy at all times, with some further research it is really helpfull!
Especially when it comes to the dependency injection some questions arise and I hope you have the time to help me out.
1) The module for the domain layer is missing. But I guess this is, because the annotated @Inject in the interactors are sufficient for dagger to build the graph. A module for the data layer was only necessary, because it references an interface and we have to tell Dagger which implementation to use. Am I right so far?
2) The interactors use @Inject for the constructor but no scope is annotated to the interactor class. As far as I understand it, this means that there could be multiple instances of the interactors. No @Singleton (@MyCustomScope) is annotated and therefore Dagger creates a new instance everytime an interactor object is requested. Is this right, or did I miss a point?
Thanks in advance,