The past and present of Android project framework


Hi, everyone. It’s sunny today. It’s a wonderful start.
This article tells you about the three popular frameworks of Android, the past and present of the architecture, and my understanding and views on these frameworks.

Differences among the three frameworks


  • Architecture introduction

Model: data model, for example, we get data from database or network
View: view, our XML layout file
Controller: controller, our activity

  • Model connection

View --> ControllerThat is to say, some user events (click and touch events) of view are reflected on the activity.
Controller --> ModelIn other words, the activity reads and writes some data we need.
Controller --> ViewThat is, after the activity obtains the data, it reflects the updated content to the view.

Such a complete project architecture came out, which is also a common project architecture for our early development.

  • Advantages and disadvantages

This shortcoming is quite obvious. The main performance is that our activity is too heavy. We often write hundreds or thousands of lines.
The reason for this problem is that the relationship between the controller layer and the view layer is too close, that is, there are too many code to operate the view in the activity.

But! But! I don’t think Android’s default development structure is traditionalMVCStructure, because an activity can be called a view layer or a layerControllerLayer, so we can’t tell the level in detail. We just do some encapsulation, and then activity carries all the layers.
Of course, this is mePersonal viewWe can discuss them all.


  • Architecture introduction

It wasn’t becauseActivityThere are operations in view, but also doControllerDo you work?
So in fact, MVP architecture starts from the original activity layerviewandControllerSeparate, separate out a layerPresenterAs the originalControllerMy position.
Then, the view layer is written as an interface, and the activity implements the view interface. Finally, the view layer is written as an interfacePresenterClass.

Model: data model, for example, we get data from database or network.
View: view, our XML layout file and activity.
PresenterHost, a separate class, only do scheduling work.

  • Model connection

View --> PresenterTo reflect some user events of view to the presenter.
Presenter --> Model, presenter to read and write some data we need.
Controller --> ViewAfter obtaining the data, the presenter feeds back the updated content to the activity to update the view.

  • Advantages and disadvantages

The advantage of this method is that it greatly reduces the burden of the activity. The activity mainly undertakes the work of updating the view, and then transfers the work of interacting with the model to the userPresenterAnd thusPresenterTo control and interact with the model side and the view side. So let the project more clear and simple, sequential thinking development.

The disadvantages are also obvious
First of all, the amount of code has greatly increased. Every page or function point needs to write a presenter class. Because it is interface oriented programming, it needs to add a large number of interfaces, and there will be a lot of cumbersome callbacks.
Second, becausePresenterThe activity object is held in the view object, so it may lead to memory leak or null pointer of view. This is also something to be noted.


  • Architecture introduction

MVVMThe feature of jetpack is two-way binding, with official blessing from Google. It updates many architecture components in jetpack, such asViewModel,Livedata,DataBindingSo this is the mainstream framework and the official framework.

Model: data model, for example, we get data from database or network.
View: view, our XML layout file and activity.
ViewModel: association layer, binding model and view, so that they can bind each other and update in real time

  • Model connection

View --> ViewModel -->View, bidirectional binding, data changes can be reflected in the interface, interface changes can be reflected in the data.
ViewModel --> ModelTo manipulate some of the data we need.

  • Advantages and disadvantages

The advantage is the official support, so also updated a lot of related database, let you knowMVVMThe architecture is stronger and better to use, and the feature of two-way binding can save us a lot of interaction between view and model. Also basically solved the above two architecture problems.

Talk about my understanding of MVVM

Let’s talk about how MVVM solves the defects and problems of the other two architectures

  • It solves the problem that the coupling degree between different levels is too highWhich means better decoupling. In MVP layer, the presenter will still hold the reference of view, but in MVVM, view and model are bound Bi directionally, so that ViewModel only needs to deal with business logic, and does not need the elements related to the relational interface.

  • It solves the problem of too much code or too much patterned code。 Because of bi-directional binding, UI related code is much less, which is also the key to less code. One of the key components is data binding, which makes all UI changes be handed over to the observed data model.

  • Solved the possible memory leak problem。 One of the components of MVVM architecture is livedata, which has the ability of life cycle awareness. It can sense the life cycle of activities, etc., so it can be cleaned up after its associated life cycle is destroyed, which greatly reduces the problem of memory leakage.

  • The problem of view null pointer caused by activity stop is solved。 If livedata is used in MVVM, if the observer’s life cycle is inactive (such as returning activity in the stack) when it needs to update the view, it will not receive any livedata events. In other words, it will ensure that it will respond only when the interface is visible, which solves the problem of null pointer.

  • It solves the problem of life cycle management。 This is mainly due to the lifecycle component, which enables some controls to observe the lifecycle, and can carry out lifecycle events anytime and anywhere.

Let’s talk about responsive programming

Responsive programmingTo put it bluntly, I first construct the relationship between things, and then I can leave it alone. They will drive each other because of this relationship.
In fact, this is what we often call observer mode, or subscription publishing mode.

Why do you say this? BecauseMVVMThe essence of thinking is similar to this. Whether it’s two-way binding or life cycle perception, it’s actually an observer mode, which makes everything observable. Then we just need to stabilize the observation relationship, and the project will be stable.

Finally, why is MVVM so powerful?

Personally, I think,MVVMIt’s not because of the architecture itself, but because of the advantages of this kind of responsive programmingGoogleWith the strong support of the government, there are so many supported components to maintain the MVVM architecture. In fact, the government wants to unify the project architecture.

Excellent architecture thought + official support = strong


There are small partners who can study together and pay attention to them ❤️ My official account, the block on the code, asks three questions every day, and analyzes them in detail to help you become offer harvesters.