Mobile application development, front end and backend development

Android MVP Architecture

Android MVP Architecture

MVP (Model-View-Presenter) is one of well know design architecture for android application development.

Why need MVP

MVP separate the application UI logic from the business logic. At the beginning of Android development, we write most of our codes in activity Classes. But when the application group it will hard to maintain the codebase. Also it hard to test each functionality individually because most of the functionalities are tightly couple with the activity class itself.

As an example let’s think we have an application to save our todo list to the local database and retrieve from the local database. Then later we need to change the data retrieve layer to get data from API. If we write this data get functionality inside the activity itself, we need to change a lot of codebase inside the activity if we change the data retrieving code. But if we write that using MVP architecture we don’t need to change the UI layer. We need to change only the data retrieve layer.

Components in MVP

In MVP are 3 main components.

Model – Model contain business logic and provide data to show in view. It include API call, database handle etc.

View – Has a reference to the activity. We call the interface methods when we need to trigger actions.

Presenter – This act as a glue between the Model and the View. Presenter view format the data to show in the view. Presenter contains a reference to the view and we call the interface method inside presenter.

Implementation

In this article, I’m gone a show you a basic example. In here user will be able to enter username and password and we validate the username and password and show whether the password and username are correct or not.

First, we need to create an interface to act as a View. In here I am gone add two methods to show valid and Invalid login.