从零理解 Activity 启动流程

从零理解 Activity 启动流程

AMS → ActivityThread → Activity 完全解析

目录

  1. 整体流程概览
  2. Launcher 启动 Activity
  3. AMS 处理阶段
  4. ActivityThread 执行阶段
  5. Activity 生命周期回调
  6. 关键类作用

1. 整体流程概览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
┌─────────────────────────────────────────────────────────────────────┐
│ Activity 启动完整流程 │
├─────────────────────────────────────────────────────────────────────┤
│ │
│ App Process │
│ ──────────┬───────────────────────────────────────────── │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ startActivity() │ │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────┐ System Server │
│ │ IActivityTask │ ──────────────────────▶ │
│ │ Manager │ Binder 通信 │
│ └────────┬────────┘ │ │
│ │ ▼ │
│ │ ┌─────────────────┐ │
│ │ │ AMS │ │
│ │ │ (ActivityTask) │ │
│ │ └────────┬────────┘ │
│ │ │ │
│ │ ▼ │
│ │ ┌─────────────────┐ │
│ │ │ ActivityStarter│ │
│ │ │ 解析 Intent │ │
│ │ │ 决定启动模式 │ │
│ │ └────────┬────────┘ │
│ │ │ │
│ │ ▼ │
│ │ ┌─────────────────┐ │
│ │ │ Application │ │
│ │ │ Thread │ │
│ │ └────────┬────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────┐ │ │
│ │ ActivityThread │◀─────────────────────────────┘ │
│ │ H 发送消息 │ │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ performLaunch │ │
│ │ Activity onCreate │ │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────┘

2. Launcher 启动 Activity

Activity.startActivity()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Activity.java
public void startActivity(Intent intent) {
startActivity(intent, null);
}

public void startActivity(Intent intent, Bundle options) {
// 实际上是调用 instrumentation
mInstrumentation.execStartActivity(
this,
mMainThread.getApplicationThread(),
mToken,
this,
intent,
-1,
options
);
}

Instrumentation 转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Instrumentation.java
public ActivityResult execStartActivity(
Context who,
IBinder contextThread,
IBinder token,
Activity target,
Intent intent,
int requestCode,
Bundle options
) {
// 通过 Binder 通知系统服务
int result = ActivityTaskManager.getService()
.startActivity(whoThread, who.getBasePackageName(), intent,
intent.resolveTypeIfNeeded(who.getContentResolver()),
token, target != null ? target.mEmbeddedID : null,
requestCode, 0, null, options);

checkStartActivityResult(result, intent);
return null;
}

3. AMS 处理阶段

ActivityTaskManagerService (ATMS)

1
2
3
4
5
6
7
8
9
10
// ActivityTaskManagerService.java
public final int startActivity(IApplicationThread caller,
String callingPackage, Intent intent, String resolvedType,
IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo,
Bundle bOptions) {
return startActivityAsUser(caller, callingPackage, intent, resolvedType,
resultTo, resultWho, requestCode, startFlags, profilerInfo,
bOptions, UserHandle.getCallingUserId());
}

ActivityStarter 解析

1
2
3
4
5
6
7
8
9
10
11
12
13
// ActivityStarter.java
int startActivityLocked(...){
// 1. 解析 Intent
// 2. 检查权限
// 3. 决定启动模式 (standard, singleTop, singleTask, singleInstance)
// 4. 处理 task 相关逻辑
// 5. 检查目标 Activity 是否已在运行

// 最终调用
return startActivityUnchecked(r, sourceRecord, voiceSession,
voiceInteractor, startFlags, true, checkedOptions, inTask,
restrictedBgActivity, intentGrants);
}

ActivityStack 任务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
// ActivityStack.java
private int startActivityUnchecked(...) {
// 根据启动模式决定:
// - 是否创建新 Task
// - 是否复用已有 Activity
// - 是否清除栈顶

// 最终调用
mRootActivityContainer.startActivityLocked(r, newTask,
doResume, checkedOptions, inTask, restrictedBgActivity);

return ActivityManager.START_SUCCESS;
}

4. ActivityThread 执行阶段

ApplicationThread 接收

1
2
3
4
5
6
7
8
// ActivityThread.ApplicationThread
public void scheduleTransaction(ClientTransaction transaction) {
// 通过 H Handler 发送消息到主线程
ActivityThread.this.sendMessage(
ActivityThread.H.EXECUTE_TRANSACTION,
transaction
);
}

H Handler 处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// ActivityThread.java
class H extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
mTransactionExecutor.execute(transaction);
break;

case LAUNCH_ACTIVITY:
final ActivityClientRecord r = (ActivityClientRecord) msg.obj;
handleLaunchActivity(r, null, null);
break;
}
}
}

handleLaunchActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private Activity handleLaunchActivity(ActivityClientRecord r,
Intent customIntent, ActivityInfo activityInfo,
Configuration overrideConfig, CompatibilityInfo compatInfo,
String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean isForward, boolean probe, ActivityStack activityStack) {

// 1. 创建 Activity
Activity activity = performLaunchActivity(r, customIntent);

// 2. 恢复状态
if (activity != null) {
handleResumeActivity(r.token, false,
r.isForward, "", r.procState, null);
}

return activity;
}

performLaunchActivity 核心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// 1. 获取 ComponentName
ComponentName component = r.intent.getComponent();

// 2. 创建 Activity 实例 (通过 ClassLoader)
Activity activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);

// 3. 创建 Application (如果还没有)
Application app = r.packageInfo.makeApplication(false, mInstrumentation);

// 4. 创建 Context
Context appContext = createBaseContext(activity, r);

// 5. 关联 Context
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback);

// 6. 调用 onCreate
if (r.state != null) {
r.state.setClassLoader(activity.getClassLoader());
}
mInstrumentation.callActivityOnCreate(activity, r.state);

return activity;
}

5. Activity 生命周期回调

onCreate 调用链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
performLaunchActivity


Instrumentation.callActivityOnCreate(activity, bundle)


Activity.performCreate(bundle)


Activity.onCreate(bundle)


Activity.onStart()


Activity.onResume()

Activity.attach() - 创建 Window

1
2
3
4
5
6
7
8
9
10
11
12
// Activity.java
final void attach(Context context, ActivityThread thread, ...) {
// 创建 PhoneWindow
mWindow = new PhoneWindow(this, windowStyle, parentWindow, activityConfigCallback);

// 设置 WindowManager
mWindowManager = appContext.getSystemService(Context.WINDOW_SERVICE);

// 保存组件
mToken = token;
mThread = thread;
}

onCreate 中 setContentView 流程

1
2
3
4
5
6
7
8
9
10
Activity.setContentView(layoutResID)


PhoneWindow.setContentView(layoutResID)

├─▶ 1. 创建 DecorView

├─▶ 2. 解析 layoutResID 生成 View 树

└─▶ 3. 将 View 放入 ContentFrameLayout

6. 关键类作用

作用
ActivityTaskManagerService 系统服务,管理 Activity 任务
ActivityStarter 解析 Intent,决定启动模式
ActivityStack 管理 Activity 栈
ApplicationThread App 端Binder服务端
ActivityThread 主线程,消息循环
H Handler,切换到主线程执行
Instrumentation 监控 Activity 生命周期
ActivityClientRecord Activity 客户端记录

面试常问问题

Q1: Activity 启动为什么要通过 Binder?

1
2
3
4
5
答案:
1. Activity 运行在 App 进程
2. AMS 运行在 System Server 进程
3. 不同进程间通信需要 Binder
4. 这是 Android 进程间通信的基础机制

Q2: Activity 启动为什么慢?

1
2
3
4
5
6
7
8
9
10
答案:
1. 需要与 AMS 进程通信 (Binder)
2. 需要创建 Application、Context、Window
3. 需要解析 Layout、创建 View 树
4. 需要执行生命周期方法

优化:
- 使用 ViewBinding 替代 findViewById
- 延迟初始化
- 使用 Coil/Glide 延迟加载图片

Q3: onCreate 和 onResume 区别?

1
2
3
4
5
6
7
8
onCreate: Activity 创建时调用,只调用一次
- 初始化 UI
- setContentView

onResume: Activity 可见并可交互时调用
- 每次回到 Activity 都调用
- 刷新 UI
- 启动动画

总结

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Activity 启动关键点:

1. startActivity()

2. Instrumentation.execStartActivity()

3. Binder → ATMS.startActivity()

4. ActivityStarter 解析启动模式

5. ApplicationThread.scheduleTransaction()

6. H Handler 处理 (切换到主线程)

7. performLaunchActivity() 创建 Activity

8. callActivityOnCreate() → onCreate()

9. handleResumeActivity() → onResume()

相关文章