The Forgotten Behavior Equation

Can a single equation depict the whole working of our behavior? Is that simple to simplify them? No. But this equation contains everything you need to know about building good habits, shaping your…

Smartphone

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




7 steps to implement Dagger 2 in Android

I chose to build a simple movies app to test the dagger implementation. This is the app.

For those of you interested in skipping the article, the GitHub link is for this implementation is here.

So let’s begin!

Next thing we have to do is setup Room database in the app. This involves:

Now that we have setup the local database, we need to configure the api service system. We are going to use Retrofit for that. This involves:

The repository classes are responsible for handling data operations. For instance, the first time the app is opened, the data will be fetched from the backend api service and stored locally in room. But if there is no internet or the api service is down, the data will be fetched from the local cache. So this class will choose which data sources to use to get the data.

As mentioned in the previous step:

Now, let’s setup the ViewModel class.

Now let’s create some Dagger classes.

In Dagger, we can annotate classes with @Module. These classes are responsible for providing objects/classes which can be injected. Such classes can define methods annotated with @Provides. The returned objects from these methods are available for dependency injection.

So in our case, we need to inject two classes: MovieDao class and MovieApiService class. So we are going to create two Modules: ApiModule and DbModule (This can be done in a single module but I prefer to keep local and web service separate).

Now we need to inject these two modules into our ViewModel. ViewModelFactory class basically helps you dynamically create ViewModels for your Activities and Fragments. The ViewModelFactoryclass 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.

ViewModelKeys helps you map your ViewModel classes so ViewModelFactory can correctly provide/inject them.

The ViewModelModule is used to provide a map of view models through dagger that is used by the ViewModelFactory class.

So basically,

Any class with the annotation @Component defines the connection between the modules and the classes which need the dependency. We define a @Component.Builder interface which will be called from our custom Application class. This will set our application object to the AppComponent. So inside the AppComponent the application instance is available. So this application instance can be accessed by our modules such as ApiModule when needed.

So now we create a custom Application class in our project.

Note: Don’t forget to add this custom Application class to the AndroidManifest.xml

So now we need to create our Activity class.

And that’s it! We build and run our app and this is the output we should receive.

We saw how to inject the ViewModelFactory into our MainActivity. If you would like to inject the ViewModelFactory into a Fragment, we need to do the following modifications:

Create a new class called FragmentModule.

Add the newly created FragmentModule to whichever Activity you want to inject. i.e. in our case MainActivity.

We need to implement HasSupportFragmentInjector in our Activity if we want to inject the ViewModelFactory class in our Fragment. And also, move all our RecyclerView.Adapter and ViewModel implementation to the Fragment class.

Lastly, we need to create our Fragment class. We inject the ViewModelFactory into the Fragment and initialise the viewModel. The remaining implementation is the same.

And that’s it! 😄. I know it feels overwhelming and kinda a lot of work but in the end it’s worth it, I think.

You can find the GitHub link for this sample implementation here:

Fragment injection implementation can be found under:

I hope you enjoyed this article and found it useful, if so please hit the Clap button. Let me know your thoughts in the comments section.

Happy coding! Thanks for reading!

Add a comment

Related posts:

A Scenic Route through PySpark Internals

I had been using Apache Sparks’s Python API, AKA PySpark, for over an year when one day, I ran into this error: Usually this leads to me trolling through similar errors on Stack Overflow, trying the…

Mi primera experiencia como Embajadora

Hace un tiempo apliqué para ser Embajadora (colaboradora) de @jompeame un proyecto dominicano que a través de una plataforma digital capta fondos para brindar asistencia a distintas causas sociales…

WebTotem or how we want to make the Internet safer.

Every day hackers infect more than 30,000 websites in the world. On the internet we see websites hacks as we have seen sex epidemic in the 80s. Nobody uses basic protection. That is the main reason…