Basic introduction of activity from creation to display in Android

preface

Speaking of the activity in Android, if you have done IOS development, the activity is similar to the viewcontroller in IOS. Everything you can see in the application is put in the activity. Activity is an important thing in Android development and an entry point for user interaction and data. The content of this blog is the creation of activities, the jump of activities and the transparent transmission of values.

The viewcontroller in IOS also has its own life cycle. It is necessary to understand the life cycle of activity or viewcontroller. This article will introduce you in detail about the activity in Android from creation to display, and share it for your reference and learning. I won't say much below. Let's take a look at the detailed introduction together.

Activity is the most commonly used component in our daily development. It is necessary for us to understand the creation and display process of activity, which should be our reserve knowledge.

Creation of activity

The process of creating and initializing an activity is in the activitythread#performlaunchactivity method. In this method, there are the following key points,

This is where you can see a small part of the activity lifecycle. We need to learn some of these points, and there are some very important operations in these points.

Let's not talk about the process of creating an activity, but direct reflection. Let's focus on the attach method,

Activity#attach

The attachment code is as follows

In the attach method of activity, the key point is to initialize window. From here, we can see that the implementation class of window is phonewindow. The creation of phonewindow is very important for our subsequent operations.

Activity#onCreate

In activity.performcreate, the oncreate method of activity will be called, which is very familiar in our normal development. In oncreate, we call setcontentview to fill the layout and perform some initialization operations

setContentView

In setcontentview, which we are quite familiar with, we will call the setcontentview method of phonewindow. Let's take a brief look at the setcontentview of phonewindow

In the setcontentview method of phonewindoe, decorview will be initialized and the layout we set will be loaded into the contentparent. We won't talk about the specific logic of installdecor here.

Resume procedure

There are two key points in the activitythread#handleresumeactivity method.

Performresume of the activity will be called in performresumeactivity, and onresume will be called in performresume, and then enter the onresume declaration cycle

Let's focus on addview and subsequent processing.

addView

WM here is WindowManager, which is initialized through setwindowmanager in the attach method. The corresponding instance is an instance of windowmanagerimpl. Then, let's take a look at the addview method of windoemanageimpl. In this method, we directly call the addview method of windowmanagerglobal, and our concern is transferred. The most critical diam are the following lines.

First create a viewrootimpl, and then setview. The code of viewrootimpl#setview method is long. We can find the requestlayout method. Go in and have a look.

Here, the first thread check is performed.

Choreographer posts a callback, which is the core of view refresh. Let's look at the run method of traversalrunnable,

In dotraversal, the performtraversals method will be called again. Let's see what the performtraversals method does. This method is very, very long, but in this method, there are key methods such as performmeasure, performlayout, performdraw, etc. so far, the three major processes of view will be displayed in front of us.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>