Hello Folks, In this article we will discuss what is MVP in android and how we can use it to make our code better organised and unit/integration tested (video series on android instrumentation testing). There are mainly three components involved in MVP pattern Model, View and Presenter. Lets discuss about each of them in detail.

What is MVP in android?

You might have seen the android code examples. Mostly you will find all the code written in the activity itself. Which makes the code less readable and hard to test. MVP helps us to separate the concerns of various parts of the code and keep it organised. Take a look at the below diagram

Crepe

Let’s discuss about all the components of MVP in android one by one.

View

In android MVP, a view contains two things:

  1. Activity - android resource
  2. View - java interface

Activity Implements the View and it injects itself(View interface) in the presenter so that presenter can talk to activity using view interface. First three blocks of the diagram shows the communication between View and The Presenter.

Presenter

Presenter acts as a middle layer between View and Data/Model. View(Activity) commands presenter to present something and presenter then takes data from the database/Model and gives back the presentable form of data to the View. View then takes care of displaying that data on the screen. And remember that Presenter is a plain java class it should not include any of the android components otherwise it will make the unit testing of the presenter hard.

If you wish to use database in the presenter then make activity create a database instance and inject it in the presenter. This will help you to mock the database while unit testing and will enable you to test the business logic.

Model

Model in MVP is nothing but your data source. View does not talk to data directly instead it commands Presenter to handle data for it and give the information back which can be displayed without any further modification.

Example of application built using MVP

Recently I built an app called ExpenseManager to manage my expenses and its a prefect example of MVP in android. Have a look at the code and the tests.

Thats all folks, If you have any questions or feedback, please leave a comment.