This section describes the application of Model-View-Presenter in Android and modelviewpresenter.

Source: Internet
Author: User

This section describes the application of Model-View-Presenter in Android and modelviewpresenter.

This article was translated when I learned the MVP model. The original article was written by Konstantin Mikheev.
Due to the limited level of English, translation is very blunt, basically rely on Google, please forgive me. The following is a translation.

In this article, I will introduce the best practices of MVP mode in Android step by step through the simplest example. At the same time, I will introduce a library that makes the MVP mode simple in Android development.

Simple? How can we benefit from this? What is MVP

ViewThe layer is used to display data and corresponding data operations. In Android, it may be Activity, Fragment, View, or Dialog.

ModelA layer is a data access layer, such as database API or remote server access API.

PresenterThe layer provides data connection between the View layer and the Model layer. The Presenter layer can also control background tasks.

In Android, MVP mode can remove background threads from Activitys/Views/Fragments to make them more independent in most lifecycle events. Such an application becomes simpler, the stability of the entire program is improved by more than 10 times, the application code becomes fewer, the Code maintainability becomes more friendly, and the developer's life becomes more happy.

Why is MVP in Android 1: Keep simple dummies

If you have not read this article, read it again: The Kiss Principle

· Most Android programs only use the View-Model mode. · Programmers need to participate in the View complexity, rather than solving the business.

In the application, you only use Model-View, and the final state is "all connections.

If the figure in this example does not look so complex, consider that each View may disappear and appear at any time. Do not forget to save and restore the view status. For temporary View attache several background tasks, cake ready!

Another "Everything Connects everything" is the object of God.

The local object is too complex; the code block cannot be reused for testing or convenient debugging and refactoring.

Use MVP Mode

· Complex tasks are divided into simple tasks and easy to solve. · Smaller objects, fewer bugs, and simpler debugging. · Testable.
Cause 2: Background task

Whenever you write Activity, Fragment, or custom View, you can associate all methods with the external or static class of the background task. In this way, your background tasks will not be associated with an Activity, and will not cause memory leakage or consumption by Activity. My name is "Presenter ".

There are several methods for processing background threads, but they are not reliable, but MVP is reliable.

Why can MVP

This view shows what happens when configuration changes or memory overflow occurs for different application controls. Every Android developer should know this view, but not every developer knows this view.

Case 1:When a user rotates the screen, changes the language settings, attache an external monitor, and so on, the Configuration usually changes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.