Analyze the startup process of Android activity

Analyze the startup process of Android activity

For the startup process of Android activity, I have read the Android source code for a long time. The following is the startup process of activity I sorted out and share with you:

As one of the four major components of Android and the most basic component, activity is responsible for all functions of interacting with users. The startup process of activity is not a mysterious thing. Next, let's simply analyze the startup process of activity from the perspective of source code.

Root activity generally refers to the mainactivity in our project, which represents an Android application and is generally started in a new process. In the Android system, all activity components are saved in the stack. We start a new activity component, which is located above the previous activity. So what is the process of opening an app from the launcher, as shown below:

(1) The launcher sends a request to the activitymanagerservice to start mainactivity; (2) Activitymanagerservice first saves the relevant information of mainactivity, and then sends a request to the launcher to make it enter the abort state; (3) After receiving the abort status, the launcher will send a request to the activitymanagerservice that has entered the abort status, so that the activitymanagerservice can continue to start mainactivity; (4) Activitymanagerservice checks the process used to run mainactivity. If it does not exist, start a new process; (5) After the new application process is started, it will send a start completion request to the activitymanagerservice to facilitate the activitymanagerservice to continue to start mainactivity; (6) The activitymanagerservice sends the mainactivity related information saved in step (2) to the newly created process so that the process can start the mainactivity component.

Launcher.startActivitySafely

When we click the application icon on the launcher, the startactivitysafe method is called. The activity information to be started is saved in intent, including action, category, etc. So how does launcher get the information in intent? First, when the system is started, a management service called packagemanagerservice will be started and applications in the system will be installed through it. In this process, packagemanagerservice will parse the application configuration file androidmanifest.xml to obtain the component information in the program (including activity, service, broadcast, etc.), Then packagemanagerservice queries all activities whose action is "Android. Intent. Action. Main" and category is "Android. Intent. Category. Launcher", then creates a shortcut icon for each application and associates the program information with it. In the above code, the start flag bit of activity is set to "intent.flag_activity_new_task", so that he can start in a new task.

Activity.startActivity

Call startactivityforresult. If the second parameter (requestcode) is - 1, it means that the result does not need to be passed back when the activity is closed.

Activity.startActivityForResult

It is not difficult to find that in the end, it actually calls mimstrumentation.execstartactivity to start the activity. The type of mimstrumentation is instrumentation, which is used to monitor the interaction between the program and the system. Mminstrumentation executes the startup operation of the activity instead, so that he can monitor this interactive process.

The type of mmainthread is activitythread, which is used to describe an application process. Every time the system starts a program, an instance of activitythread will be loaded in it, and the instance will be saved in the member variable mmainthread of the activity, while mmainthread. Getapplicationthread() is used to obtain a local binder object of type applicationthread. The type of mtoken is ibinder, which is a binder proxy object. Only a local binder object of activityrecord in activitymanagerservice is considered. Each started activity has a corresponding activityrecord object in the activitymanagerservice, which is used to maintain the running status and information of the activity.

Instrumentation.execStartActivity

The above code shows that we can get a proxy object of ActivityManagerService through ActivityManagerNative.getDefault (), and then call his startActivity method to notify ActivityManagerService to start Activity.

There are also a series of processes in the middle. It is not difficult to find that it follows the source code. Finally, it calls the schedulelaunchactivity of applicationthread to start the activity.

Application.scheduleLaunchActivity

The main thing of the above code is to construct a ActivityClientRecord, then call sendMessage to send a message. In the process corresponding to the application, each activity component is described by an activityclientrecord object, which is saved in the member variable m activities of the activitythread class. So how does the handler handle this message?

H.handleMessage

First, transform the obj in MSG into a ActivityClientRecord object, then call to get a LoaderApk object and save it in the member variable packageInfo of the ActivityClientRecord object. The loader object is used to describe an APK file that has been loaded. Finally, call handleLaunchActivity to start the Activity component.

ActivityThread.handleLaunchActivity

At this point, it's very clear. Hold your breath here and suddenly relax ~ ~ let's see what performlunchactivity does ~ ~ performlunchactivity function loads the derived class of user-defined activity and executes its oncreate function, which will return this activity object.

ActivityThread.performLaunchActivity

The token in the activityrecord is a binder proxy object. Like the activityclientrecord object, it is used to describe the started activity component, but the former is used in the activitymanagerservice and the latter is used in the application process.

At this point, the activity startup process is analyzed. The startup process of mainactivity can also be regarded as the startup process of application. The startup process of the child activity is similar to that of the root activity. The process is as follows:

(1) Mainactivity sends an automatic childactivity request to activitymanagerservice;

(2) Activitymanagerservice first saves the information of childactivity, and then sends an abort request to mainactivity;

(3) After receiving the request, mainactivity enters the abort state and tells activitymanagerservice so that activitymanagerservice can continue to start childactivity

(4) Activitymanagerservice checks whether the process running by childactivity exists, and sends childactivity information to him for startup. In terms of source code, the principle is similar. Compared with mainactivity, it will be a little simpler. It will not be described in detail here. You can read the source code according to the previous steps.

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>