Preface I wonder if you have ever thought about this question: in daily development, the most commonly used method is to call up a new Activity through startActivity(). Who holds the reference to the created Activity object? The newly started Activity object should always be referenced during its life cycle, otherwise it will be recycled during the system GC. So what is the reference relationship? In order to figure out the whole problem, the author began to search the source code (Android Q). First, I had to figure out how the Activity instance was created. Creation of Activity object The launch of an Activity is a cross-process communication process. For the client, the creation of an Activity will call back to the handleLaunchActivity() method in ActivityThread:
Then I found the creation of the Acitivity instance in the performLaunchActivity() method:
The creation of Activity is handled by the Instrumentation class:
The final creation work is further implemented by the factory class AppComponentFactory:
At this point, the process of creating an Activity object is already very clear: get the Class object of the target Activity through the ClassLoader object and the class name, and then call the newInstance() method of the Class object to create an instance. The graphical relationship is as follows: Reference relationship of Activity object After understanding the creation process of the Activity object, let's go back to the performLaunchActivity() method of the ActivityThread at the beginning and then look down:
Here we seem to have found the answer we were looking for: The newly created Activity object will be held by the passed in ActivityClientRecord object, and then the ActivityClientRecord object will be added to a collection called mActivities. ActivityClientRecord is a static inner class of ActivityThread, used to record information related to Activity. The object creation process can be found in the LaunchActivityItem class (after API 28): frameworks/base/core/java/android/app/servertransaction/LaunchActivityItem.java:
Let's take a look at this mActivities collection: frameworks/base/core/java/android/app/ActivityThread.java:
mActivities is a map collection, which is a member variable of the ActivityThread object. Since it is a collection, you can naturally find the operation of removing elements in the collection in the Activity destroy method callback:
The graphical relationship is represented as follows: Since the Activity object is indirectly referenced by the ActivityThread object, the ActivityThread object should exist as a singleton. So how is the singleton ActivityThread object created and held? Creation of ActivityThread object When a new application process is created, the static main method main() of ActivityThread is called. Here, we found the answer: frameworks/base/core/java/android/app/ActivityThread.java:
From the above code, we can see that when a new application process is created, a new ActivityThread object is created in the main() method and assigned to a static member variable sCurrentActivityThread of the ActivityThread class, thus forming a relationship in which one application process corresponds to one ActivityThread object (singleton). Summarize Each newly started Activity, after its object instance is created by the newInstance method of the Class class, is wrapped in an ActivityClientRecord object and then added to the member variable mActivitys of the process's only ActivityThread object. In other words, the holding and release of Activity objects are managed by ActivityThread. Finally, I would like to reiterate two additional points: In the source code, the Activity object has a transfer relationship in multiple methods, which is quite complicated. The author is not very knowledgeable and may have missed some other important reference relationships without analysis. Everyone is welcome to correct me. The framework source code above uses the latest Android Q version before the deadline. The relevant source code of this part will be modified for different Android system versions. I cannot compare and analyze them in detail one by one. I hope you can forgive me. |
<<: What happens from URL input to page display?
I have a friend~ I have built a small website, an...
Here are 8 ways to fix a slow Internet connection...
80VPS is offering a promotion for some cluster se...
With the acceleration of cloud migration and the ...
On August 6, Huawei's online seminar (Huawei ...
People sometimes mistakenly use the terms “web sc...
As 2020 winds down and the new year dawns, it pro...
Expensive 5G plans After entering 2020, 5G has be...
Power over Ethernet (PoE) is a revolutionary tech...
Network monitoring is one of the most important n...
Now there is a new WIFI standard that can increas...
[[420464]] There is a magical phenomenon in this ...
I wonder what you think 5G should look like? Fast...
Why does the front-end need to understand the HTT...
[[182802]] According to a report released by mark...