Skip to main content

Ways to Adopt Model View Presenter on Android








In this tutorial, we explore the Model View Presenter pattern in more detail by implementing it in an Android application.
In this tutorial:
  • we build a simple application using the MVP pattern
  • we explore how to implement the MVP pattern on Android
  • and we discuss how to overcome some difficulties caused by Android's architecture
The Model View Presenter pattern is an architectural pattern based on the Model View Controller (MVC) pattern that increases the separation of concerns and facilitates unit testing. It creates three layers, ModelView, andPresenter, each with a well defined responsibility.









Model View Presenter Layers

The Model holds the business logic of the application. It controls how data is created, stored, and modified. The View is a passive interface that displays data and routes user actions to the Presenter. The Presenter acts as the middleman. It retrieves data from the Model and shows it in the View. It also processes user actions forwarded by the View.
We are going to build a simple notes application to illustrate MVP. The app allows the user to take notes, save them in a local database, and delete notes. To make it simple, the app will have only one Activity.









App Layout

In this tutorial, we concentrate primarily on the implementation of the MVP pattern. Other functions, such as setting up a SQLite database, constructing aDAO, or handling user interaction, are skipped. If you need help with any of these topics, Envato Tuts+ has some great tutorials about these topics.
Let's start with the creation of a new note. If we break this action into smaller operations, then this is what the flow would look like using the MVP architectural pattern:
  • The user types a note and clicks the add note button.
  • The Presenter creates a Noteobject with the text entered by the user and asks the Model to insert it in the database.
  • The Model inserts the note in the database and informs the Presenter that the list of notes has changed.
  • The Presenter clears the text field and asks the View to refresh its list to show the newly created note.
MVP Action Diagram
Let's now consider the operations needed to achieve this action and separate them using MVP. To keep the various objects loosely coupled, the communication between the layers takes places by using interfaces. We need four interfaces:
  • RequiredViewOps: required View operations available to Presenter
  • ProvidedPresenterOps: operations offered to View for communication with Presenter
  • RequiredPresenterOps: required Presenter operations available to Model
  • ProvidedModelOps: operations offered to Model to communicate with Presenter
MVP Interfaces
Now that we have an idea of how the various methods should be organized, we can start creating our app. We simplify the implementation by only focusing on the action to add a new note. The source files of this tutorial are available on GitHub.

We use only one Activity with a layout that includes:
  • EditText for new notes
  • Button to add a note
  • RecyclerView to list all notes
  • two TextView elements and aButton inside aRecyclerView holder
Let’s begin by creating the interfaces. To keep everything organized, we place the interfaces within a holder. Again, in this example we focus on the action to add a new note.
It's now time to create the Model, View, and Presenter layers. Since MainActivity will act as the View, it should implement the RequiredViewOps interface.
The Presenter is the middleman and needs to implement two interfaces:
  • ProvidedPresenterOps to allow calls from the View
  • RequiredPresenterOps to receive results from the Model
Pay special attention to the View layer reference. We need to use a WeakReference<MVP_Main.RequiredViewOps> sinceMainActivity could be destroyed at any time and we want to avoid memory leaks. Also, the Model layer hasn't been set up yet. We do that later when we connect the MVP layers together.

Comments

You may also want to read these ⤵️

Referee kills player in a football match

A referee is facing murder charges after football players allegedly forced him to

Do not watch this while driving

Kids are lovely and fun to watch most times. I know most of you did this and so many other funny stuffs as a kid. Feel free to share yours... Do not watch this while driving

The Pros and Cons Of COVID-19 Contact Tracing Apps

                Written by                Jack -  Guest author Contact tracing apps seem to be the new fad. Their popularity is also one that comes on the back of a series of unfortunate events. They seem to hold another approach to fighting the menace of a disease that has claimed no less than 400,000 lives worldwide. The widespread race to get these apps working can also be attributed to the coming together of two big names in tech, Apple and Google, to make the frameworks for such apps happen. Like every other thing, though, how does the scales tip in favor of, or against, these apps? The Case for Contact Tracing Apps In April alone, the US people lost more jobs than the economy had been able to gain in about a combined decade. More unemployment claims are filed daily, while companies do not take their staff out of furlough anytime soon. Businesses are folding up daily, too, especially those still p...

Over 40 Million Accounts Found Guilty

Microsoft has uncovered 44 million user accounts using usernames and passwords that have been leaked through security breaches.