MVP in android

      4 Comments on MVP in android

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

MVP in android

Lets discuss about all the components of MVP in android one by one.

View(example – activity, view interface)

In android MVP, a view contains two things
Activity – android resource
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 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 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.

4 thoughts on “MVP in android

  1. Tim

    Suppose your Model represents some data about the user (maybe just their name), and suppose there is a EditText and Button in the Activity where the EditText displays the user’s name and the Button saves the user data.

    If the user changes the text in the EditText and rotates the phone without hitting the save Button, how does that data persist to the next instance of the Activity if the View is not to interact with the Model?

    1. Ajit Singh Post author

      This particular scenario can be handled differently. There is a configuration in AndroidManifest.xml which tells android that on orientation change persist the data present on view in a temporary storage. And when the activity is restarted put the data back on the view.

      Here is that config. Put it in the AndroidManifest.xml and it will start working.

      1. Tim

        Thanks for your reply, Ajit. I’m not sure I like this solution. This solution may solve the problem for screen rotations, but if Android decides to reclaim an Activity sitting on the Activity stack, the problem is still not solved.

        The only thing I can think is to make all Models and all Presenters implement Parcelable and have the Activity pass the Presenter through onSaveInstanceState.

        1. Ajit Singh Post author

          In that case, we can still use the onSaveInstanceState() method. And based on the requirement you can delegate the task of updating the Bundle to the Presenter or to a separate plain java class which is responsible for the restore of the activity and have test for it.


Leave a Reply

Your email address will not be published. Required fields are marked *