In this tutorial, we’ll be implementing MVP pattern along with Dagger2 in our Android Application. It’s recommended to go through each of these design pattern concepts individually before using them together.

Android MVP and Dagger2

We know that MVP is based on the separation of concerns. MVP pattern separates our code into three layers – Model, View and Presenter.

Presenter acts as a bridge between the Model and the View. Model and View cannot communicate with each other WITHOUT the Presenter.

Presenter-Model, Presenter-View communicate using Interfaces.

Dagger2 is a Dependency Injection Library developed by Square. Instead of creating dependencies in our classes, we provide them by using the Dagger2 core structure – Module, Component, Inject and Provides.

Using the Dagger2 annotations, the compiler builds the Dagger classes automatically at compile-time.

MVP + Dagger2 = Beginning our Journey to write clean code that follows the SOLID Principles.

So in the following section, we’ll use Dagger2 in our MVP Android Studio Project from this tutorial. This would give us a clarity as to how Dagger2 works with MVP.

Android MVP Dagger2 Project Structure

android-mvp-dagger2-project-structure

model – The Model class of the Project.

view – Our activity is the view here.

presenter – The presenter class, which ultimately tells the Activity what to do with the View.

MainContract.java – Consists of the interfaces for the Model, View and Presenter.

di – Consists of Dependencies Injection – Dagger2 classes.

Don’t forget to add the following dependencies in your build.gradle file.

Android MVP Dagger2 Code

Let’s look at the interfaces defined for the MVP classes in our MainContract.java class.

So the View has interfaces for setting a string, showing or hiding a ProgressBar.

The Model has an interface onFinished which triggers after the handler interval succeeds.

The Presenter controls the activity button click and onDestroy() lifecycle.

Model.java

The presenter implementation is defined in the MainPresenterImpl.java class.

di package

In the di package we define the dependency injection modules and components.

Let’s look at the Modules first.

AppModule.java

This provides an application instance across the project.

ContextModule.java

This provides getApplicationContext()

DataModule.java

This provides an instance of Model class for us.

MvpModule.java

This provides an instance of the MainPresenterImpl.java class.

It takes the View’s interface and the Model class as it’s constructor arguments.

ActivityScope.java

This sets the scope for the components which we’ll see next.

AppComponent.java

This is the App level Component. The fields would be accessible throughout the application.

ActivityComponent.java

This Component is used to inject the Presenter in our MainActivity.

The code for the InitApplication.java is given below:

The code for the activity_main.xml layout file is given below.

The code for the MainActivity.java class is given below:

Voila! We inject the Presenter in here without instantiating it. Thanks to Dagger2. Now compare this activity source code with the one here.

The output of the above application in action is given below.

android mvp dagger2 android app output

This brings an end to MVP and Dagger2 integration tutorial. You can download the AndroidMVPDagger2 Project from the link below.

By admin

Leave a Reply

%d bloggers like this: