There are a lot of people who have read a lot of MVP articles and understand the principles, but just can’t write MVP code or write code that is completely copied from someone else’s template. It’s fine to copy it, but if you want to make a simple change to the template, you don’t know where to start? This post is for the new MVPS and the young programmers.
For those of you who can read English, you can read this article and the comments below, and you’ll find out more about MVP writing.

Reference article: MVP for Android: How to Organize the presentation Layer: antonioleiva.com/mvp-android…

1. Why MVP mode?

Answer: The MVP pattern allows (ˈsepəreɪt ɪt) The presentation layer from The logic
For an application to be easily extensible([EK ‘stensɪ BL] extensible) and maintainable([mein’teinə BL] maintainable),we need to define well-separated layer.
Separating interface from logic in Android is not easy,but the MVP pattern makes it easier to prevent our activities end up degrading into very coupled classes consisting of thundreds or even thouds of lines.

2. Why borrow androidmvp code?

Answer: 5344 stars on Github, no ISSUSS; Code written succinctly and clearly; Another Kotlin-based release that supports some of the new java8 features, such as lambda expressions, etc.



3. What is MVP mode?

Answer: A pattern of uncoupling activities that divides a function into a Model layer (responsible for preparing data), a V layer (responsible for displaying data and interacting with the user), and a P layer (connecting layers M and V)

4. In what scenario?

Answer: One of the most popular patterns([‘pætənz] mode) to organize the presentation layer in Android Application.
MVP is not a architecture by itself,it’s only responsible for the presentation layer.

5. How to code MVP mode and what are the responsibilities of model View Presenters? What code do you need to write in different layers?

With MVP we take most of logic out from the activities so that we can test it
There are many variations of MVP and everyone can adjust the pattern to their need and the way they feel more comfortable.
I will show how I usually work, but I want this article to be more a place for discussion rather than strict guidelines on how to apply MVP, because up to there is no “standard” way to implement it.
The Model: only be the gateway to the domain layer or business logic.it is enough to see as the provider of the data we want to display in the view.

Implemented by an Activity(It may be a Fragment,a View… dependingon how the app os structured),will contain a reference to the presenter.

The only thing that the view will do is calling a presenter method every time there is a user action(a button click for example)



Then, the activity can implement those methods .
The view uses the presenter to notify about user interactions.

Presenter:The presenter is responsible to act as the middleman between view and model.

It retrieves data from the model and returns it firmatted to the view.

6. MVP limitations?

Answer: MVP only models the presentation layer,but the rest of layers will still require a good architecture if you want a flexible and scalable App.


An example of a complete architecture could be Clean Architecture ghough there are many other options.


MVP has some risks, and the most important we use to forget is that the presenter is attached to the view forever. And the view is an activity, which means that:
  • We can try to update activities that have already died
Imagine you send a request to a server that takes 10 seconds, but the user closes the activity after 5 seconds. By the time the callback is called and the UI is updated, it will crash because the activity is finishing.
To solve this ,we call the onDestory() method that cleans the view:


Reference items: github.com/antoniolg/a…


Finally, I offer some official information about the model. I suggest that you read the explanation on the official website directly, which is much better than reading others’ blogs, because others add their own understanding when writing blogs, which is bound to deviate from the explanation on the official website.
If English reading is not enough, then read more, and slowly you will find it will be of great help to the future of the program.
The MVVM with architecture components:antonioleiva.com/architectur…
The Clean Architecture:fernandocejas.com/2015/07/18/…
Uncle Bob ‘s clean architecture:8thlight.com/blog/uncle-…
Dagger:
square.github.io/dagger/
Kotlin-for-Android-Developers:https://github.com/antoniolg/Kotlin-for-Android-Developers
Kotlin online course:antonioleiva.com/online-cour…