Android-Model View Presenter
Model-View-Presenter is a derivation of MVC (Model View Controller) architectural pattern. The purpose of the MVP is to attain a clean separation between business layer and view layer of an application. It reduces much of the logic that is placed in the View (Activity and fragments) by making use of a Presenter. Presenter class is likely to handle the presentation logic and update the view accordingly. MVP mainly concerns on the separation of business logic out of the activity or fragment in order to have minimal interaction with view.

Let’s see more about what each component does:
Model
- The model contains the data in a simple class that we need to display it in the view.
- The model is the gateway to the domain layer or business logic. It basically is the provider of the data to be shown by the view.
View
- View is implemented using an Activity or a Fragment depending on how the app is structured.
- Presenter method is called every time when there is a user action(for instance when a button is clicked a particular method from a presenter is invoked). So, this component is responsible to display the data that is given by the Presenter.
- As already mentioned above View and Presenter should have one to one mapping. It is ideal to have single presenter for each view.
- View should get data only from the Presenter and not from the Model component.
- In order to have a clean code, it is necessary that the View should make use of Presenter’s interface in order to communicate with its presenter. The purpose of interface is to decouple the parts of the architecture. Also, it contains list of all possible actions the presenter can perform (e.g. void showLoading()).
Presenter
- It acts as a middle man between the View and Model layer.
- As specified above in the View section, it is advisable that the presenter to use View’s interface to communicate its view in order to have a cleaner code.
- Presenter mainly should contain only java code and reduce having any Android framework code so that it can be used later for different types of views and can be tested easily without making use of an Android device.
- Presenter is notified in the data change and the view recieves data through the Presenter and update itself using data that is sent through the presenter.
- Upon the user interaction with a view, a method in the presenter is invoked. Presenter’s responsibility is to call an appropriate method for that action to update the model.
Key points:
- Basically, View and Presenter have one-to-one mapping. It is possible to have multiple presenters for complex views.
- Presenter acts as mediator between Model and View, where View is more separated from Model.
- Using an interface, the code can be tested easily and it is decoupled from any specific implementation. Interface forms a contract between the presenter and view. So it is advisable that the presenter and view to communicate using an interface but it is not mandatory to use interface.
- MVP provides a clean and maintainable code. It makes much easier and faster to test.
MVP Sample App
Here is a simple app following a MVP pattern. Project is structured in different package. It looks like the below figure.

Contract
IMainContract
Project has an Interface called IMainContract which contains presenter (IMainPresenter) and view (IMainView) interfaces. IMainPresenter extends a IBasePresenter and IMainView extends a IBaseView.
The interface IBaseView<P extends IBasePresenter> should be implemented by all the views in your app. The view is given a generic type P for the presenter and they must all contain a setPresenter() method(Presenters get registered to view).

Activity
MainActivity.java
This activity implements IMainContract.IMainView interface. It contains a simple screen with few EditText and a button to save the details.
Presenter
MainPresenter.java
Here, MainPresenter.java is a middle man between view and model. This implements the interface IMainContract.IMainPresenter. When a button is clicked, method from a presenter is invoked to show a Toast on a view.
Note:
Creating Presenter in Fragment is not a good practice. Because they are separate layers. It is illegal for Separation of concerns. Also, if we create presenter in Fragment, we bind Presenter’s life to view’s LifeCycle and when Fragment is destroyed and recreated, we also create a new presenter but they are in different layers. (For more info: Stack overflow)
Please find the project in GITLAB.
Output:
