Android MVP — what a hell is that ?

josenaves
We love Android
Published in
3 min readMar 17, 2016
Your code without MVP

No, I’m not talking about Most Viable Product. For this article context, MVP is an acronym for Model View Presenter. Yes, I am talking about an architecture for an Android app.

First of all, this article is not intended to be a guide for your MVP implementation. Go on and make a search here in Medium and you find out plenty implementations.

First things, first. Let’s go the concepts.

What the hell is a Presenter ?

The Presenter has the responsibility to be the “middle-man”. It lays between the View and the Model.

The Presenter:

  • Loads data from the model
  • Listens to user actions (in the view) and model updates (database, APIs, etc)
  • Updates the model and the view
  • Is in charge of any formatting, localization, etc.
  • Handles all the background tasks
  • Must not have any references to Android SDK classes.

Keep that in mind — Presenter talks to the View and the Model (always).

And what about the View?

The View is responsible for “low level details“. In Android, we are talking about UI. Normally, the View is represented by activities, fragments (or any other android.view.View or Viewgroup).

The View has a (weak) reference to the Presenter. At any user interaction with the UI, the View will call a method on Presenter. The presenter can “push” data onto View.

Here, we don’t have any references to the model. By the model, I’m not talking about a simple POJO (like a transfer object). It’s ok if you have them in your View.

And the Model?

The Model is your data and your business logic. It doesn’t matter where it lives. The data can be in a database in your phone, in the cloud accessible via an API, it doesn’t matter.

See the Model as a gateway to all the data your app needs.

Again, your Model is not a POJO !

The whole picture

View ← →Presenter → Model

Wrapping up

Your code with MVP

To finish the post, I could put some code here but that’s not the purpose of this article. I want you to stop and think about what you read here.

After some time, try to draw some diagrams, or even write some code. That will be your own MVP implementation. Don’t worry about any MVP frameworks out there — just write what you need and try to make sense.

The first version could be a little messy. Don’t worry, with time, you will refine it.

--

--

josenaves
We love Android

Software engineer {Android, React Native, iOS, Java, Javascript, Go}.