background

The startup process of Android applications is an inextricable problem that not only helps us develop, but also improves our technical vision from the perspective of the operating system.

Phase 1: System startup

1. First, Android startup.

The first process is zygote. Zygote means “fertilize soft” in English. Why is fertilization soft? Because all processes of the system are from zygote process fork. The first process started by Zygote is the well-known SystemServer process. This process contains what we call three great system services: ActivityManagerService, WindowManagerService, and PackegeManagerService.

2. This is followed by the process startup of our application.

The process entry is in the ActivityThread class’s main () method, which is a program entry similar to C’s mian method. The main method does a few things: <1 initializes our mainLopper and mainHander so that the UI thread messaging mechanism is turned on. <2 Calls the attach () method of ActivityThread.

Phase 2: App registration

Attach () method of ActivityThread

This method then calls getDeafault() of ActivityManagerNatvie (a singleton class that can get an instance of ActivityManagerService), returning the ActivityManagerService instance. Then call ActivityManagerService. AttachApplication (mAPPThread) method. This mAPPThread is an instance of ApplicationThread.

ApplicationThread

ApplicationThread is the inner class of ActivityThread. It is the entry point for the App to interact with the system across processes. Its implementation class is in the client process. Includes life cycle callbacks. ApplicationThread’s implementation class in the App process interacts with ActivityThread across threads via the H handler to complete the final call-back of the lifecycle. These time types are: The life cycles of the LAUNCH_ACTIVITY and RESUME_ACTIVITY and Servivce are also handled by the Handler H, which is the UI thread.

Why is Servivce in the UI thread?

This question has been answered for a while because the lifecycle callback to H this Handler is in the UI thread.

4. ActivityManagerService. AttachApplication (mAPPThread) method

In addition, from the system’s point of view, which is AMS’s point of view, each App has a process, which is stored in a data structure called ProcessRecord. Each Activity in the app uses an ActivityRecord data structure, and each Activity corresponds to a Token. This Token is also a Binder object that uniquely identifies an Activity.

5. Call mAPPThread. BindApplication (ProcessRecord) parameters, such as

This is a cross-process call that actually synchronizes App information to the ProcessRecord for system management.

6. ActivityStackSupervisor. AttachApplicationLocked ProcessRecord () method

This class is an AMS stack management class that stores the ActivityStack collection. In this method, you iterate through each ActivityStack, finding the foreground stack, and finding the TopActivity in it. And compare the incoming ProcessRecord processName and UID is a the same ActivityRecord inside and opActivity to use. If consistent, call ActivityStackSupervisor. RealStartAcvitiyLocked (ProcessRecord ActivityRecord) method.

7. ActivityStackSupervisor. RealStartAcvitiyLocked (ProcessRecord ActivityRecord) method.

This method calls the ScheduelLaunchActivity (including ActivityRecord) method of the passed ApplicationThread instance.

Stage three: Start the first Activity

8. ApplicationThread. ScheduelLaunchActivity (including ActivityRecord) method.

This method is cross-process and synchronizes the ActivityRecord to the App process’s ActivityRecordClient data structure, which is then used to construct applications, activities, etc.

9. Send H an H.launch_activity message

10. Call the HandleLaunchActivity () method of ActivityThread.

The PerformLaunchActivity and HandleLaunchActivtiy () methods are then called. # Stage 4: Display screen This stage will be discussed in detail later, but I will briefly mention it here.

PerformLaunchActivity do

1. This method is mainly the Application of construction and by mInstrumention newActivity () tectonic Activity. 2. Call the Activity attach (application ContextImpl)

Activity attach (Application ContextImpl, etc.)

This method initializes a Window, and I’ll talk more about that later, but personality views are attached to the docorView of a Window, and then displayed by wMS.AddView.

HandleLaunchActivtiy

This method calls Actiity’s resume () method, And call wMS.addView (window) inside makrVisible (). The contentView of the docorView in Windows is setContentView (int) inside onCreate () Layout).

And it shows up

Note that wMS.AddView (Window), the system service, which we’ll cover next time, has a class RootViewImpl that manages the hierarchical drawing of our contentView view tree.