Explain in detail how the android.activity destruction process works

Continuing with our source code analysis, in the previous article, we introduced the startup process of activity. A typical scenario is that activity a starts an activity B. their life cycle callback methods are:

We have also verified such a life cycle call sequence according to the source code. What about the destruction process of activity? What is the calling sequence of its life cycle?

Here we make a simple demo, let a Activity a start Activity B, and then call finish () method in B, their life cycle execution sequence is:

Well, according to the callback process of the life cycle method we tested, we start to analyze the activity destruction process. Generally speaking, when we need to destroy an activity, we will call its own finish method, so our process starts with the finish method.

1: Request to destroy the current activity

First, we call the finish method in our own activity, which actually calls the finish method of the activity:

Then we can find that it calls the overloaded method of the finish method and passes a parameter value:

Well, this parameter value doesn't seem to work... This is not discussed here, and then the ActivityManagerNative.getDefault ().FinishActivity method is invoked. Well, according to the introduction of the previous article, we know that ActivityManagerNative is a Binder object, and the method that is invoked will eventually be executed by ActivityManagerService. Therefore, the final execution of the finishactivity is activitymanagerservice. Finishactivity method. Well, let's take a look at the execution logic of the finishactivity method of activitymanagerservice...

Here we can find that after a series of logical judgments, the requestfinishactivitylocked method of activitystack is finally called. Here should be the logic for executing finish activity.

The finishactivitylocked method is called in the method body. Let's continue to look at the implementation of finishactivitylocked method:

Well, the startpausinglocked method is called here. The name should be the onpause method request to execute the activity. Then let's take a look at the implementation of the startpausinglocked method:

In this way, the finish method is called from the application, and the activitymanagerservice receives the request and executes the startpausinglocked method.

2: Executes the onpause method of the current activity

In the method startpausinglocked, we called prev.app.thread.schedulepauseactivity. What we actually called here is the schedulepauseactivity method of iaapplicationthread. Iaapplicationthread is also a binder object, which is the binder client end of applicationthread in the activitythread, so the schedulepauseactivity method of applicationthread will be called in the end, OK, let's take a look at the specific implementation of the schedulepauseactivity method of activitythread:

Then we call the sendMessage method of ActivityThread:

Then the overloaded method of SendMessage is called back..

Finally, call MH to send asynchronous messages, then process asynchronous messages in the handlemessage method of MH and call the handlepauseactivity method:

Well, the performpauseactivity method is called back here. We analyzed this code in the previous article:

So we call back the first lifecycle method: onpause...

In the handlepauseactivity method, we call the activitymanagernative. Getdefault(). Activitypaused (token) method. Well, it is the method of callback activitymanagerservice, which will eventually call the activitypaused method of activitymanagerservice:

In this way, let's continue to look at the implementation of the activitypausedlocked method:

After a series of logical judgments, the completepauselocked method is executed:

In this way, the onpause operation of the activity at the top of the stack is completed, and then the onresume operation of the previous activity is started...

3: Performs the onresume operation of the previous activity

In this way, the activitystacksupervisor.resumetoperactivitieslocked method is called.., Start calling this method again. Through the introduction of the previous article, we know that this method actually performs the initialization of activity. Let's take a look at its specific calling process:

Well, this process has already been introduced in the previous article, so we won't do too much analysis here. By calling this process, we finally execute the onrestart method, OnStart method, onresume method, etc. of the previous activity on the current top of the stack. Next, we will call the onstop method and ondestory method of the top of the stack activity.

4: Execute the destroy operation of stack top activity

We call Looper.myQueue ().AddIdleHandler (New Idler ()) in the ActivityThread.handleResumeActivity method. Let's take a look at the implementation of this method.

Internally, there is a callback method for queueIdle. When it is added to MessageQueue, it will call back the method. We can find that the ActivityManagerNative.getDefault.activityIdle method is invoked in this method body. Through the last article and the above explanation, we should know that the final call is ActivityManagerService.activityIdle method, OK. Let's take a look at the specific implementation of the activityidle method:

It can be found that the activitystacksupervisor.activityidleinternallocked method is called here. Then let's take a look at the specific implementation of activityidleinternallocked method:

You can see that the activitystack.destroyactivitylocked method is called here. You can see its specific implementation:

Well, the iaapplicationthread.schedulestudyactivity method starts to be executed again. As explained above, the activitythread.schedulestudyactivity method is finally called here. Well, take a look at the implementation of activitythread.schedulestudyactivity method:

Here we start to execute the SendMessage method. Through a series of calls to the SendMessage method, we finally call the handledestroyactivity method:

You can see that the performdestroyactivity method is called here to execute the ondestroy method of avtivity:

Then we call the Activity.performStop () method to see the performStop method:

Then the Instrumentation.callActivityOnStop () method is called:

Well, I finally called the onstop method of activity...

Let's continue with instrumentation. Callactivityondestroy().... The ondestroy method of activity is called again through instrumentation:

Then take a look at the implementation of the performdestroy () method of activity:

O (∩ ∩) O ha ha ~, finally called back the ondestroy method of activity....

Summary:

The destruction process of an activity starts with the finish method

The activity destruction process is: onpause C > onrestart C > OnStart C > onresume C > onstop C > ondestroy

The destruction process of an activity is coordinated by activitythread and activitymanagerservice

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>