18

One of the most up to date samples covering Android Architecture Components is GithubBrowserSample provided by Google. I reviewed the code and a few questions arose:

  1. I have noticed that ViewModelModule is included in AppModule. It means that all the viewmodels are added to the DI graph. Why that is done in that way instead of having separate Module for each Activity/Fragment that would provide only needed ViewModel for specific Activity/Fragment?

  2. In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel? Or the better solution would be to create a setter in ViewModel and set needed param via setter?

1

1 Answer 1

11
+50
  1. [...] It means that all the viewmodels are added to the DI graph. Why that is done in that way instead of having separate Module for each Activity/Fragment [...]?

They are added to the DI graph, but they are not yet created. Instead they end up in a map of providers, as seen in the ViewModelFacory.

@Inject
public GithubViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { }

So we now have a GithubViewModelFactory that has a list of providers and can create any ViewModel that was bound. Fragments and Activities can now just inject the factory and retrieve their ViewModel.

@Inject
ViewModelProvider.Factory viewModelFactory;

// ...later...
repoViewModel = ViewModelProviders.of(this, viewModelFactory).get(RepoViewModel.class);

As to the why...alternatively you could create a ViewModelProvider.Factory for every Activity / Fragment and register the implementation in every Module. This would be a lot of duplicated boilerplate code, though.

  1. In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel? Or the better solution would be to create a setter in ViewModel and set needed param via setter?

It seems like all the ViewModels only depend on @Singleton objects—which is necessary, since they all get provided from the AppComponent. This means that there is no way to pass in "parameters" other than other @Singleton dependencies.

So, as you suggested, you'd either have to move the factory down into the Activity / Fragment component so that you can provide lower-scoped dependencies, or use a setter method.

4
  • With earlier dagger2 version (without dagger.android) I used subcomponents. In so in activity I could do App.get(context).appComponent.plus(MyModule("12345")).inject(this). So in this module I could instantiate factory with param ViewModelProvider.Factor("12345") that could create me ViewModel with param. How I can do the same using dagger.android injection? Sep 19, 2017 at 10:41
  • I have found this approach github.com/Nimrodda/dagger-androidinjector/blob/master/app/src/… Maybe that is the way Sep 19, 2017 at 10:58
  • @VadimsSavjolovs keep in mind that the android parts are still in @Beta. Currently there is no support for non-default-constructor modules, so the link you posted seems like a possible approach Sep 19, 2017 at 11:22
  • > As to the why...alternatively you could create a ViewModelProvider.Factory for every Activity / Fragment and register the implementation in every Module. Actually that what i do, you may got pb with your DI graph otherwise, for example providing a per fragment injection in your viewmodel may leading to dagger error "inject need a @provides annotation..." Oct 21, 2017 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.