ActivityThread and ApplicationThread, the bridge between the main thread of the Android application and AMS communication

ActivityThread and ApplicationThread, the bridge between the main thread of the Android application and AMS communication

ActivityThread

ActivityThread is an important component in the Android system that is responsible for managing and scheduling Activities in applications.

Essence and function:

  • ActivityThread is essentially a thread that is used to start the application and perform the main loop of the application in the process. It can be understood as the main thread or UI thread of the application.
  • Manages the execution of the main thread of the application process and is responsible for scheduling and executing activities, broadcasts, and other operations according to the requirements of AMS (through the IApplicationThread interface, AMS is the Client, ActivityThread.ApplicationThread is the Server).

Main Responsibilities:

  • Handling the Activity life cycle: ActivityThread is responsible for detecting and executing the life cycle methods of each Activity, such as onCreate(), onStart(), onPause, etc., and controlling the creation, start, stop and destruction of the Activity according to the life cycle state.
  • Manage message loop: ActivityThread creates a message loop mechanism to handle messages and events in Activity, and processes and distributes messages according to priority. The messages created include user events in the user interface, Intent and Broadcast requests, etc.
  • Managing windows and views: responsible for managing the creation and display of window views, as well as event handling related to windows and views.

Initialization process:

  • In the main() method, ActivityThread initializes some global states, such as creating an Application object and an Instrumentation object.
  • ActivityThread creates an H (Handler) object and creates a message loop (Looper) through the H object.
  • ActivityThread will call the callApplicationOnCreate() method of the Instrumentation object to notify the App's Application object to initialize. This is also the first callback function in the App.
  • ActivityThread will call the execute() method of the ActivityClientRecord object to start the related operations of starting the Activity.
  • ActivityThread will call the life cycle methods of Activity in sequence, such as onCreate(), onStart(), onResume(), etc.

Member variables and methods:

  • The ActivityThread class contains many important member variables and methods, such as mLooper (representing the message loop used by the main thread), mActivities (a collection of currently active (Activity) objects), mH (Handler object, processing messages in the message queue), and mInstrumentation (Instrumentation object, used to perform various test and measurement operations).

The main responsibilities of the main thread are:

  • In the Android system, by default, all components in an application (such as Activity, BroadcastReceiver, Service) are executed in the same process, and the main thread of this process is responsible for execution. The main thread's main responsibility is to quickly handle UI events.

ApplicationThread

ApplicationThread is an inner class of ActivityThread and is a Binder object. ApplicationThread acts as the server side of the IApplicationThread object, waiting for client requests and then processing them. The largest client is AMS.

ApplicationThread plays an important role in the Android application startup process. When the handleBindApplication method of ActivityThread is called, ApplicationThread will be bound to the Application object as a bridge for communication between the application and system services. The handleBindApplication method will also call the onCreate method of the Application object, which is the first callback method in the application startup process.

In the process of starting the Activity, the ScheduleActivity method of ApplicationThread sends a message to start the Activity through a Handler called H. ActivityThread is also known as the UI thread or main thread. It is the entry point of the program (main function) and saves all the corresponding components of the application (such as activities, services, and content providers, etc.).

Create the root Activity:

picture

The Launcher process requests AMS to create a root Activity. AMS determines whether the application process required by the root Activity exists and starts it. If it does not exist, it requests the Zygote process to create the application process. After the application process is started, AMS requests the application process to create and start the root Activity.

Create a normal Activity:

picture

The Activity in the application process requests AMS to create a normal Activity (step 1). AMS will manage the life cycle and stack of this Activity, verify (AndroidManifest.xml registration file declaration, start permission) Activity, etc. If the Activity meets the AMS verification (judgment process exists), AMS will request the ActivityThread in the application process to create and start a normal Activity.

picture

Summarize

ApplicationThread is an inner class of ActivityThread, which implements the IApplicationThread interface, a Binder interface used for inter-process communication (IPC). As the server of IApplicationThread, the ApplicationThread object is called by system services such as AMS through the Binder mechanism.

ActivityThread is the main thread of the Android application process, which is what we usually call the UI thread. Each Android application process has an ActivityThread instance, which is responsible for creating and managing application components (such as Activity, Service, BroadcastReceiver, etc.) and handling life cycle events related to these components.

ApplicationThread and ActivityThread are closely related. ApplicationThread is an inner class of ActivityThread, which is used to handle IPC calls from AMS and convert these calls into calls to corresponding methods in ActivityThread. AMS communicates with ActivityThread through ApplicationThread to manage the life cycle and components of the application.

<<:  Is there a bug in iOS 17.5? Users’ private photos reappear after being deleted!

>>:  Android process management: How to terminate the process during development

Recommend

Ogilvy's Brand Marketing Model and Strategy

This time, I would like to share with you Ogilvy ...

Marketing node reminder in January 2018 [Dry goods collection]

The long 2017 is finally over, and we are about t...

Complete the entire calculus course in 19 hours

Course Description The Calculus video tutorial co...

Precautions for fresh Cordyceps sinensis

May and June every year are the fresh Cordyceps s...

Cancer is contagious, but not in the way you think

"Viral" and "contagious" canc...

Why are your conversions always so low?

As bidders, we may also be familiar with the term...