ActivityManagerService Part 2: Activity Start Procedure

We walk through the start process of an Activity. During the journey,  we will see important structure representing Activity, Process, Stack and Task. The detailed information regarding stack and task were described in Android developer website. I will only focus on the key method which is related to starting activity. Trivia logic will be skipped.

Phase 1: Create Data Structure For Target Activity

Let’s see the code flow below.

圖片1

According to above sequence chart, the first important method which is worthy to be discussed is “startActivityMayWait“. The main task of it is collecting the target activity information via package manager then maintain the information by ActivityInfo.java. ActivityInfo is then utilized in next phase.

圖片2

In continued phases, activity managed classes are adopted. Theses classes include ActivityRecord, TaskRecord and ActivityStack (see here for concept). The class dependency is as below figure.

圖片3

Let’s continue the code flow. Now, two major objects managing activity are created.

圖片5

A new created task was then put at the top of stack. Refer to following code snippet.

圖片6

Phase 2: Create Process

In phase 1, we collected information for Activity and created vessels (ActivityRecord and TaskRecord) for it. Next step is to try to “resume" the activity to show. Before resuming target activity, current activity who launched target activity should be put into ground. Yes, it should enter “paused" state. Let’s see how it works.

圖片7

resumeTopActivitiesLocked is the main entry point to phase 2. I summarize two major tasks in resumeTopActivitiesLocked method in below figure. We don’t discuss the activity pausing flow here. I left it to We focus on starting activity.

圖片8

While starting a new activity, system will try to find an existed process or just create a new one.

圖片10

Now, system creates a new process. The newly created process is only a vessel. It is not related to the target activity yet.

圖片11

Following code snippet indicates an important method of “bindApplication“. We skip it here. Focus on start activity flow.

圖片12

圖片13

Phase 3: Activity Ready to GO

In this phase, an Activity object is created and ready to GO.

圖片14

圖片15

Summary

It’s a very long journey to walk through the activity start process. I don’t plan to end here. I will discuss details of bindApplication and performLaunchActivity in next article.

發表留言