MVP Pattern on Android
In this article I will explain how to create a basic application with WVP pattern, all the code is available here: Gitlab repository.
For this tutorial I asume that you are already familiar with Android development and the java programming lenguaje, so I won't explain the most basic things about creation an Android application or basics in java programming. For basic information there are many good sources of information in internet like Udacity.
What is MVP?
MVP stands for Model View Controller, this pattern helps to separate all the related functionality from how it is represented on the screen. That means that it would be possible to replace the design with cero extra work, since all the logic is in a different layer.
MVP project example
This project contains a small example on how create an Android application using the MVP pattern, for this I use one of the most common functionalities in an application, the login process.
How the example application was created?
First create a project in AndroidStudio with the following packages
Inside the control package create the following classes
ControlActivity is the start point of the application and it basically send the user to the login screen. It will implements the ControlView interface and will create the ControlPresenterImpl instance.
The ControlPresenterImpl will hace a reference to the activity and will have the logic related to the screen, is this case there is no screen, so it will only contains the intent to the login
Now the login process, this will show how is the interaction between the layers. In the login package create the following classes and interfaces.
The LoginActivity will implement the LoginView interface which provides all methods that will be used for communication between the view and the presenter, like show the login errors for example.
The LoginPresenterImpl will implement the LoginPresenter interface, which will handle the events from the view and will call the methods that actually have some functionality in the interactors.
Complete code can be found https://github.com/LycanDev/MVP_pattern
Principal Software Engineer @ R2
7 年Great hands-on start point for android MVP pattern Alexander Cabezas Medina, I think that this code could be the foundation to teach something more advanced, like DI using dagger. Anyways, I'm just giving you some ideas since I can see that you're having fun while writing android tutorials :D, regards !