启动过程分析

Author Avatar
MoonChen 8月 08, 2017

先上一段关键方法调用栈:

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
## Activity启动
ContextImpl.startActivity(Intent intent)
startActivity(Intent intent, Bundle options)
mMainThread.getInstrumentation().execStartActivity(); [Instrumentation]
ActivityManagerNative.getDefault().startActivity(...); [ActivityManagerNative 实际实现类是:ActivityManagerService]
startActivityAsUser()
mStackSupervisor.startActivityMayWait) [ActivityStackSupervisor]
startActivityLocked()
startActivityUncheckedLocked() [启动模式判断]
## find activityStack [ActivityStack]
ActivityStack.resumeTopActivityLocked()
resumeTopActivityInnerLocked()
mStackSupervisor.startSpecificActivityLocked(next, true, true); [ActivityStackSupervisor]
realStartActivityLocked(r, app, andResume, checkConfig);
app.thread.scheduleLaunchActivity(xxx); [app.thread->IApplicationThread]
# app.thread -> ActivityThread中的ApplicationThread
sendMessage(H.LAUNCH_ACTIVITY, r);
## H -> 主线程Handler
void handleMessage(Message msg)
case LAUNCH_ACTIVITY:
handleLaunchActivity(r, null);
Activity a = performLaunchActivity(r, customIntent);
1.[Activity]mInstrumentation.newActivity(cl, component.getClassName(), r.intent);
(Activity)cl.loadClass(className).newInstance(); [Instrumentation]
2.[Application] app = r.packageInfo.makeApplication(false, mInstrumentation);
mActivityThread.mInstrumentation.newApplication(cl, appClass, appContext); [[Instrumentation]]
newApplication(cl.loadClass(className), context);
(Application)clazz.newInstance()
3.[Context] appContext = createBaseContextForActivity(r, activity);
ContextImpl.createActivityContext(xxx);
new ContextImpl(mainThread...);
4.activity.attach(appContext, this, getInstrumentation()...);
5.mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
activity.performCreate(icicle, persistentState);
onCreate(icicle);