The gay decade show

It is impossible to quit smoking while you’re forced not to drink. Especially if you find yourself somehow invited to a party, in which you know almost nobody, but know for sure you’re really into…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Injecting ViewModels using Dagger 2 with Map Multibinding on Android

ViewModel is a part of Jetpack, which is a Google product and Dagger 2 also managed by Google. So it seems easy to implement and not much workaround needed to injecting ViewModel using Dagger 2 as it both(dagger & ViewModel) owned by Google. But it’s not true you have to follow a specific process to inject ViewModel and it is through Map Multibinding. ViewModel is a good place to inject dependency because you have the Repository instance (if you follow the MVVM architecture) or other instances that access the Data classes that are coming into ViewModel. If you follow the MVVM architecture ViewModel is the perfect place to inject the dependency.

I am not going to the needy greedy details of Map Multibinding and why you can’t inject ViewModels by default. There are lots of articles out there about that. Just search the term (ViewModel Dagger, ViewModel Multibinding). You got the idea.

First sets up all the initial Dagger 2 stuff if you want to create a project from scratch by yourself. Dagger setup is very similar in any project. or you can check out my article here if DI(Dependency Injection) is new to you. Where I showed how to sets all the initial Dagger setup.

Now let's get started:

It’s nice to structure your class into a package. Create a package in your main package directory and name it viewmodels. Inside the package create ViewModelProviderFactory.java class and paste the code:

Now, What’s the point of creating ViewModelProviderFactory class?

it's because by default classes that extend ViewModel can only have an empty constructor. You can’t pass any parameter through the constructor. So if you want to use constructor injection you can’t pass parameters through a constructor. So to solve the issue we have to create custom ViewModelProviderFactory. If we create the factory then we can effectively do the injection. In the constructor of the class, you can see the @Inject annotation. That’s a particular thing that has to do with the Multibinding. It’s a known issue in the Android community as I mentioned earlier.

ui > auth > AuthViewModel.java which extends ViewModel

As you can see that there is a @Inject annotation in the AuthViewModel constructor. As of now, we don’t have any dependency injecting through this constructor. But trust me you can inject dependency in the constructor. Also, the @Inject is required because we want to inject ViewModel into Activity/Fragment. So it’s required to use in Multibinding. If it seems confusing you can understand after gone through all the steps.

di > ViewModelFactoryModule.java

This module is responsible for generating the dependency injection for the factory class created in step 1.

ui > auth > AuthActivity

Already added the dependency of ViewModel and LiveData in the build.Gradle file.

Injected bindViewModelFactory dependency(created step 3) in the AuthActivity because that’s where ViewModel is used. providerFactory is used to instantiate ViewModel.

di > ViewModelKey

Now it’s time to sets up the Multi Binding stuff. Multi Binding working in that way you create a key and you map certain dependencies to that key. You can use these dependencies and use/inject them in different classes.

di > auth > AuthViewModelsModule

This is where the multi binding is working

This module(AuthViewModelsModule) provides dependency for AuthViewModel itself. (ui > auth > AuthViewModel). Remember we have added @inject annotation in AuthViewModel constructor. We have one view model here that is AuthViewModelsModule. You can add any number of ViewModels here.

Now add two modules(ViewModelFactoryModule.class and
AuthViewModelsModule.class) we have just created to AppComponent.

All steps are done. Now it’s time to check if the ViewModel is working as expected or not…

Goto the ui > auth > AuthViewModel class and add a log…

Add a comment

Related posts:

Sell your home faster than ever

Sell my house fast in Milwaukee and surrounding areas of Wisconsin… we’d like to make you a FAIR, competitive, all-cash offer. And we’ll even GUARANTEE* that offer and can have a check in your hand…

Strength Is The Most Important Virtue

I was ranting to my buddy at work the other day about how in order to be a “good” man, you have to be dangerous first. The giraffe would not consider the mouse “good” or “merciful” for not killing…

Air Wallet Airdrop

AIR WALLET is the world’s first Airdrop & Digital Wallet application & platform changing the Airdrop Industry. Our simplified solutions to current industry issues will make applying, storing and…