Android M V P: Survival Guide

Wilfried Mbouenda Mbogne
4 min readOct 19, 2016

--

This is a survival guide for android developers new to the Model View Presenter (MVP) pattern. This is simply what you need to know about Android MVP.

What is MVP?

MVP is an application development pattern derived from the famous MVC to build user interfaces. It’s based on the separation of concerns, in particular the three concerns in the name (Model, View and Presenter).

MVP structure

Model

This represents data. It is responsible for managing data such as fetching it or saving it using APIs/services/storage.

View

This is the User interface. It handles data visualization and user interaction.

Presenter

It takes events from view or model and after business logic operations, it updates them back.

Why use MVP?

giphy.com

MVP is not the only pattern for Android Architecture so what does it make it a good choice? I will give you some pros and cons of using MVP on your android project. Based on these, you will understand if it fits to your app.

Pro:

  1. Your code is more testable. (Less Android Framework dependency.)
  2. Your app is maintainable. (Code is easy to change.)
  3. Your app is scalable. (Ability to handle growth.)
  4. Your code is independent of UI, databases.
  5. Your code is more clean. (Activities and Fragments will become shorter.)

Con:

  1. A lot of new files.
  2. A new way of thinking.
  3. Learning curve.

How to use MVP?

MVP works with 3 layers: the model, the View and the layer in the middle the Presenter. Each layer interacts just with the one near to it. The Presenter can interact with the View and the Model.

To understand how to use MVP, we need to go deeper on the role of the Model, the View and the Controller.

Model

The model handles all about the data. It answers the user requests from the presenter and handles all data sources(API, Database, files, etc). It can also notify the Presenter about data changes.

View

It informs the Presenter of relevant lifecycle events and user actions. It includes Activities, Fragments and anything that extends the Android View class.

Presenter

It contains all the logic and knowledge about what, how and when to display in the user interface. It receives the View requests based on UI events. It receives data from the Model based on data changes. It notifies the View about data to be displayed. It notifies the Model about data to be changed.

And Now what?

Some rules.

giphy.com

Now let’s see the MVP rules:

  • User interacts with the View.
  • There is one-to-one relationship between the View and the Presenter (One View is mapped to only one Presenter).
  • The View has a reference of the Presenter but has not reference to the Model.
  • The Presenter is implemented with native java code.

Show me the code.

NetflixShows is an Android application which gives you information about Netflix Tv Shows.

The code is on github: ​https://github.com/WillyShakes/NetflixShows

If you liked this post, make sure to 💚 it and follow me on twitter.

A big thanks to everyone involved in proofreading this post!

N E X T → Documenting My Android Adventure

Before you go… If you enjoyed this post, you will love to subscribe to my newsletter. Get my cheat sheet: “Android Studio keyboard shortcuts cheat sheet”.

--

--

Wilfried Mbouenda Mbogne

Senior Android Engineer, public speaker. Born in Africa, living in Europe. Work hard, play hard, rest hard www.developer-journey.com #HipHopLover,#TvShow addict