Android M V P: Survival Guide
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).
This represents data. It is responsible for managing data such as fetching it or saving it using APIs/services/storage.
This is the User interface. It handles data visualization and user interaction.
It takes events from view or model and after business logic operations, it updates them back.
Why use MVP?
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.
- Your code is more testable. (Less Android Framework dependency.)
- Your app is maintainable. (Code is easy to change.)
- Your app is scalable. (Ability to handle growth.)
- Your code is independent of UI, databases.
- Your code is more clean. (Activities and Fragments will become shorter.)
- A lot of new files.
- A new way of thinking.
- 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.
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.
It informs the Presenter of relevant lifecycle events and user actions. It includes Activities, Fragments and anything that extends the Android View class.
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?
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.
For More Information
MVP (Model View Presenter) pattern is a derivative from the well known MVC (Model View Controller), which for a while…antonioleiva.com
Nowadays MVP is a very trending topic in android community. But No matter how many advantages it offers, we doomed with…medium.com
As the Android platform has been maturing, developers found that the standard approach to organise the architecture and…davidguerrerodiaz.wordpress.com
Programming for Android may be tricky due to several reasons. One of them is the poor separation of concerns. If some…www.ackee.cz
A big thanks to everyone involved in proofreading this post!
N E X T → Documenting My Android Adventure