在開始以前,你須要知道下面幾點:java
- 有一份編譯好的 Android 源碼,如今的 AS 基本能知足,動手跟着步驟走,理解更深入
- 對 Binder 機制有必定的瞭解
- 本文基於 API 26,用什麼版本的源碼並不重要,大致的流程並沒有本質上的區別
- 從用戶手指觸摸點擊桌面圖標到 Activity 啓動
關鍵類簡介android
- ActivityManagerService:AMS 是 Android 中最核心的服務之一,主要負責系統中四大組件的啓動、切換、調度及應用進程的管理和調度等工做,其職責與操做系統中的進程管理和調度模塊相相似,它自己也是一個 Binder 的實現類,應用進程能經過 Binder 機制調用系統服務。
- ActivityThread:應用的入口類,系統經過調用main函數,開啓消息循環隊列。ActivityThread 所在線程被稱爲應用的主線程(UI 線程)。
- Instrumentation:工具類,它用來監控應用程序和系統的交互,包裝了 ActivityManagerService 的調用,一些插件化方案就是經過 hook 該類實現的。
- ActivityStarter:Activity 啓動的工具類,處理啓動 Activity 的各類 flag 。
- ActivityStackSupervisor:管理全部應用的 Activity 的棧,其中 mFocusedStack 就是當前應用的 Activity 棧。
應用進程介紹web
- 在大多數狀況下,每一個 Android 應用都在各自的 Linux 進程中運行。當須要運行應用的一些代碼時,系統會爲應用建立此進程,並使其保持運行,直到再也不須要它且系統須要回收其內存以供其餘應用使用。
- 應用進程的生命週期並不禁應用自己直接控制,而是由系統綜合多種因素來肯定的,好比系統所知道的正在運行的應用部分、這些內容對用戶的重要程度,以及系統中可用的總內存量。這是 Android 很是獨特的一個基本功能。
- 當應用組件啓動且該應用未運行任何其餘組件時,Android 系統會使用單個執行線程爲應用啓動新的 Linux 進程。默認狀況下,同一應用的全部組件會在相同的進程和線程(稱爲「主」線程)中運行。若是某個應用組件啓動且該應用已存在進程(由於存在該應用的其餘組件),則該組件會在此進程內啓動並使用相同的執行線程。可是,您能夠安排應用中的其餘組件在單獨的進程中運行,併爲任何進程建立額外的線程。
- 每一個應用進程都至關於一個 Sandbox 沙箱,Android 經過對每個應用分配一個 UID,注意這裏的 UID 不一樣於 Linux 系統的 User ID,能夠將每一個應用理解爲一個 User ,只能對其目錄下的內容具備訪問和讀寫權限。
- Android 利用遠程過程調用 (RPC) 提供了一種進程間通訊 (IPC) 機制,在此機制中,系統會(在其餘進程中)遠程執行由 Activity 或其餘應用組件調用的方法,並將全部結果返回給調用方。所以,您需將方法調用及其數據分解至操做系統可識別的程度,並將其從本地進程和地址空間傳輸至遠程進程和地址空間,而後在遠程進程中從新組裝並執行該調用。而後,返回值將沿相反方向傳輸回來。Android 提供執行這些 IPC 事務所需的所有代碼,所以您只需集中精力定義和實現 RPC 編程接口。
下面這張圖能夠補充理解一下進程的概念: 編程
先來一張流程簡圖: bash
下面是流程詳細圖,帶你看完整個啓動流程及其所涉及到的類: markdown
下面補充一張 Gityuan 大神的系統啓動架構圖幫助理解,其實只要看看這張圖的上半部分就足夠了: 架構
簡單地講,從 用戶手指觸摸點擊桌面圖標到 Activity啓動 能夠用下面 4 步歸納:app
- 當點擊桌面 App 的時候,發起進程就是 Launcher 所在的進程,啓動遠程進程,利用 Binder 發送消息給 system_server 進程;
- 在 system_server 中,啓動進程的操做會先調用
ActivityManagerService#startProcessLocked()
方法,該方法內部調用Process.start(android.app.ActivityThread)
;然後經過 socket 通訊告知 Zygote 進程 fork 子進程,即 app 進程。進程建立後將 ActivityThread 加載進去,執行ActivityThread#main()
方法;
- 在 app 進程中,
main()
方法會實例化 ActivityThread,同時建立 ApplicationThread,Looper,Hander 對象,調用ActivityThread#attach(false)
方法進行 Binder 通訊,方法裏面調用ActivityManagerService#attachApplication(mAppThread)
方法,將 thread 信息告知 ActivityManagerService , 接着 Looper 啓動循環;
- 回到 system_server 中,
ActivityManagerService#attachApplication(mAppThread)
方法內部調用了thread#bindApplication()
和mStackSupervisor#attachApplicationLocked()
咱們依次講解這兩個方法; 4.1thread#bindApplication()
方法調用了ActivityThread#sendMessage(H.BIND_APPLICATION, data)
方法,最終走到了ActivityThread#handleBindApplication()
,進而建立 Application 對象,而後調用Application#attach(context)
來綁定 Context ,建立完 Application 對象後即是調用mInstrumentation#callApplicationOnCreate()
執行Application#onCreate()
生命週期; 4.2mStackSupervisor#attachApplicationLocked()
方法中調用app#thread#scheduleLaunchActivity()
即ActivityThread#ApplicationThread#scheduleLaunchActivity()
方法,進而經過ActivityThread#sendMessage(H.LAUNCH_ACTIVITY, r)
方法,最終走到了ActivityThread#handleLaunchActivity()
,進而建立 Activity 對象,而後調用activity.attach()
方法,再調用mInstrumentation#callActivityOnCreate()
執行Activity#onCreate()
生命週期;
對應本文第一張流程圖的每個步驟,下面逐步來看看源碼是怎麼調用的:socket
用戶點擊 app 圖標;async
Launcher 捕獲點擊事件,調用 Activity#startActivity()
;
Activity#startActivity()
調用 Instrumentation;
Activity.java
@Override public void startActivity(Intent intent) { this.startActivity(intent, null); } @Override public void startActivity(Intent intent, @Nullable Bundle options) { if (options != null) { startActivityForResult(intent, -1, options); } else { // Note we want to go through this call for compatibility with // applications that may have overridden the method. startActivityForResult(intent, -1); } } public void startActivityForResult(@RequiresPermission Intent intent, int requestCode, @Nullable Bundle options) { if (mParent == null) { options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); ··· } else { ··· } } 複製代碼
Instrumentation 經過 Binder 通訊發送消息給 system_server 進程,具體 調用 ActivityManager#getService()#startActivity()
,ActivityManager#getService()
的具體實現是 ActivityManagerService ;
Instrumentation.java
public ActivityResult execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options) { ··· try { intent.migrateExtraStreamToClipData(); intent.prepareToLeaveProcess(who); int result = ActivityManager.getService() .startActivity(whoThread, who.getBasePackageName(), intent, intent.resolveTypeIfNeeded(who.getContentResolver()), token, target != null ? target.mEmbeddedID : null, requestCode, 0, null, options); checkStartActivityResult(result, intent); } catch (RemoteException e) { throw new RuntimeException("Failure from system", e); } return null; } 複製代碼
ActivityManager.java
public static IActivityManager getService() { return IActivityManagerSingleton.get(); } private static final Singleton<IActivityManager> IActivityManagerSingleton = new Singleton<IActivityManager>() { @Override protected IActivityManager create() { final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE); final IActivityManager am = IActivityManager.Stub.asInterface(b); return am; } }; 複製代碼
ActivityManagerService 調用 ActivityStarter;
ActivityManagerService.java
@Override 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()); } @Override public final int startActivityAsUser(IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) { enforceNotIsolatedCaller("startActivity"); userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(), userId, false, ALLOW_FULL_ONLY, "startActivity", null); // TODO: Switch to user app stacks here. return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, null, null, resultTo, resultWho, requestCode, startFlags, profilerInfo, null, null, bOptions, false, userId, null, null, "startActivityAsUser"); } 複製代碼
ActivityStarter 調用 ActivityStackSupervisor;
final int startActivityMayWait(IApplicationThread caller, int callingUid, String callingPackage, Intent intent, String resolvedType, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, WaitResult outResult, Configuration globalConfig, Bundle bOptions, boolean ignoreTargetSecurity, int userId, IActivityContainer iContainer, TaskRecord inTask, String reason) { ··· int res = startActivityLocked(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity, componentSpecified, outRecord, container, inTask, reason); ··· } int startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent, String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid, String callingPackage, int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container, TaskRecord inTask, String reason) { ··· mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord, container, inTask); ··· return mLastStartActivityResult; } private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent, String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid, String callingPackage, int realCallingPid, int realCallingUid, int startFlags, ActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity, ActivityStackSupervisor.ActivityContainer container, TaskRecord inTask) { ··· return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true, options, inTask, outActivity); } private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask, ActivityRecord[] outActivity) { ··· try { mService.mWindowManager.deferSurfaceLayout(); result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor, startFlags, doResume, options, inTask, outActivity); } finally { ··· } ··· } private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask, ActivityRecord[] outActivity) { ··· if (mDoResume) { final ActivityRecord topTaskActivity = mStartActivity.getTask().topRunningActivityLocked(); if (!mTargetStack.isFocusable() || (topTaskActivity != null && topTaskActivity.mTaskOverlay && mStartActivity != topTaskActivity)) { ··· } else { ··· mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity, mOptions); } } else { ··· } ··· } 複製代碼
ActivityStackSupervisor 調用 ActivityStack;
boolean resumeFocusedStackTopActivityLocked( ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) { if (targetStack != null && isFocusedStack(targetStack)) { return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions); } ··· return false; } 複製代碼
ActivityStack 回調到 ActivityStackSupervisor ;
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) { ··· try { ··· result = resumeTopActivityInnerLocked(prev, options); } finally { ··· } ··· return result; } private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) { ··· if (next.app != null && next.app.thread != null) { ··· } else { ··· mStackSupervisor.startSpecificActivityLocked(next, true, true); } ··· } 複製代碼
ActivityStackSupervisor 回調到 ActivityManagerService,這裏會判斷要啓動 App 的進程是否存在,存在則通知進程啓動 Activity,不然就先將進程建立出來;
void startSpecificActivityLocked(ActivityRecord r, boolean andResume, boolean checkConfig) { ··· if (app != null && app.thread != null) { try { ··· // 若是進程已存在,則通知進程啓動組件 realStartActivityLocked(r, app, andResume, checkConfig); return; } catch (RemoteException e) { ··· } } // 不然先將進程建立出來 mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0, "activity", r.intent.getComponent(), false, false, true); } 複製代碼
接着咱們來看看進程還沒有建立的狀況,咱們看到這裏最終調用的是 Process#start()
來啓動進程;
ActivityManagerService.java
final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName, boolean allowWhileBooting, boolean isolated, boolean keepIfLarge) { return startProcessLocked(processName, info, knownToBeDead, intentFlags, hostingType, hostingName, allowWhileBooting, isolated, 0 /* isolatedUid */, keepIfLarge, null /* ABI override */, null /* entryPoint */, null /* entryPointArgs */, null /* crashHandler */); } final ProcessRecord startProcessLocked(String processName, ApplicationInfo info, boolean knownToBeDead, int intentFlags, String hostingType, ComponentName hostingName, boolean allowWhileBooting, boolean isolated, int isolatedUid, boolean keepIfLarge, String abiOverride, String entryPoint, String[] entryPointArgs, Runnable crashHandler) { ··· startProcessLocked( app, hostingType, hostingNameStr, abiOverride, entryPoint, entryPointArgs); ··· } private final void startProcessLocked(ProcessRecord app, String hostingType, String hostingNameStr, String abiOverride, String entryPoint, String[] entryPointArgs) { ··· if (entryPoint == null) entryPoint = "android.app.ActivityThread"; Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "Start proc: " + app.processName); checkTime(startTime, "startProcess: asking zygote to start proc"); ProcessStartResult startResult; if (hostingType.equals("webview_service")) { ··· } else { startResult = Process.start(entryPoint, app.processName, uid, uid, gids, debugFlags, mountExternal, app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet, app.info.dataDir, invokeWith, entryPointArgs); } ··· } 複製代碼
ActivityManagerService 經過 socket 通訊告知 Zygote 進程 fork 子進程,即 app 進程;
進程建立後將 ActivityThread 加載進去,執行 ActivityThread#main()
方法,實例化 ActivityThread,同時建立 ApplicationThread,Looper,Hander 對象,調用 ActivityThread#attach(false) 方法進行 Binder 通訊, 接着 Looper 啓動循環;
ActivityThread.java
public static void main(String[] args) { ··· Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false); if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } ··· Looper.loop(); ··· } private void attach(boolean system) { ··· if (!system) { ··· final IActivityManager mgr = ActivityManager.getService(); try { mgr.attachApplication(mAppThread); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } ··· } else { ··· } ··· } 複製代碼
回到 system_server 中,ActivityManagerService#attachApplication(mAppThread)
方法內部調用了 thread#bindApplication()
和 mStackSupervisor#attachApplicationLocked()
這兩個方法。
其中,thread#bindApplication()
方法調用了 ActivityThread#sendMessage(H.BIND_APPLICATION, data)
方法,最終走到了 ActivityThread#handleBindApplication()
,進而建立 Application 對象,而後調用 Application#attach(context)
來綁定 Context ,建立完 Application 對象後即是調用 mInstrumentation#callApplicationOnCreate()
執行 Application#onCreate()
生命週期;
ActivityManagerService.java
@Override public final void attachApplication(IApplicationThread thread) { synchronized (this) { ··· attachApplicationLocked(thread, callingPid); ··· } } private final boolean attachApplicationLocked(IApplicationThread thread, int pid) { ··· try { ··· if (app.instr != null) { thread.bindApplication(processName, appInfo, providers, app.instr.mClass, profilerInfo, app.instr.mArguments, app.instr.mWatcher, app.instr.mUiAutomationConnection, testMode, mBinderTransactionTrackingEnabled, enableTrackAllocation, isRestrictedBackupMode || !normalMode, app.persistent, new Configuration(getGlobalConfiguration()), app.compat, getCommonServicesLocked(app.isolated), mCoreSettingsObserver.getCoreSettingsLocked(), buildSerial); } else { thread.bindApplication(processName, appInfo, providers, null, profilerInfo, null, null, null, testMode, mBinderTransactionTrackingEnabled, enableTrackAllocation, isRestrictedBackupMode || !normalMode, app.persistent, new Configuration(getGlobalConfiguration()), app.compat, getCommonServicesLocked(app.isolated), mCoreSettingsObserver.getCoreSettingsLocked(), buildSerial); } ··· } catch (Exception e) { ··· } ··· // See if the top visible activity is waiting to run in this process... if (normalMode) { try { if (mStackSupervisor.attachApplicationLocked(app)) { didSomething = true; } } catch (Exception e) { ··· } } ··· } 複製代碼
ActivityThread#ApplicationThread.java
public final void bindApplication(String processName, ApplicationInfo appInfo, ··· sendMessage(H.BIND_APPLICATION, data); } 複製代碼
ActivityThread.java
private void sendMessage(int what, Object obj) { sendMessage(what, obj, 0, 0, false); } private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { ··· mH.sendMessage(msg); } 複製代碼
咱們來看看這個 mH 的 handleMessage()
方法;
ActivityThread#H.java
public void handleMessage(Message msg) { ··· switch (msg.what) { ··· case BIND_APPLICATION: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication"); AppBindData data = (AppBindData)msg.obj; handleBindApplication(data); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; ··· } ··· } 複製代碼
建立 mInstrumentation 對象,調用 data#info#makeApplication 來建立 Application 對象;
ActivityThread.java
private void handleBindApplication(AppBindData data) {
···
try {
// 建立 Application 實例
Application app = data.info.makeApplication(data.restrictedBackupMode, null);
mInitialApplication = app;
···
try {
mInstrumentation.callApplicationOnCreate(app);
} catch (Exception e) {
···
}
} finally {
···
}
···
}
複製代碼
LoadedApk.java
public Application makeApplication(boolean forceDefaultAppClass, Instrumentation instrumentation) { if (mApplication != null) { return mApplication; } ··· Application app = null; ··· try { ··· app = mActivityThread.mInstrumentation.newApplication( cl, appClass, appContext); ··· } catch (Exception e) { ··· } ··· if (instrumentation != null) { try { //執行 Application#onCreate() 生命週期 instrumentation.callApplicationOnCreate(app); } catch (Exception e) { ··· } } ··· return app; } 複製代碼
mStackSupervisor#attachApplicationLocked()
方法中調用 app#thread#scheduleLaunchActivity()
即 ActivityThread#ApplicationThread#scheduleLaunchActivity()
方法,進而經過 ActivityThread#sendMessage(H.LAUNCH_ACTIVITY, r)
方法,最終走到了 ActivityThread#handleLaunchActivity()
,進而建立 Activity 對象,而後調用 activity.attach()
方法,再調用 mInstrumentation#callActivityOnCreate()
執行 Activity#onCreate()
生命週期;
ActivityStackSupervisor.java
boolean attachApplicationLocked(ProcessRecord app) throws RemoteException { for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) { ··· for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) { ··· if (hr != null) { if (hr.app == null && app.uid == hr.info.applicationInfo.uid && processName.equals(hr.processName)) { try { if (realStartActivityLocked(hr, app, true, true)) { didSomething = true; } } catch (RemoteException e) { ··· } } } } } ··· } final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app, boolean andResume, boolean checkConfig) throws RemoteException { ··· try { ··· app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken, System.identityHashCode(r), r.info, // TODO: Have this take the merged configuration instead of separate global and // override configs. mergedConfiguration.getGlobalConfiguration(), mergedConfiguration.getOverrideConfiguration(), r.compat, r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo); ··· } catch (RemoteException e) { ··· } ··· } 複製代碼
ActivityThread#ApplicationThread.java
@Override
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, Configuration overrideConfig,
CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
int procState, Bundle state, PersistableBundle persistentState,
List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
···
sendMessage(H.LAUNCH_ACTIVITY, r);
}
複製代碼
ActivityThread.java
private void sendMessage(int what, Object obj) { sendMessage(what, obj, 0, 0, false); } private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { ··· mH.sendMessage(msg); } 複製代碼
咱們一樣來看看這個 mH 的 handleMessage()
方法;
ActivityThread#H.java
public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what)); switch (msg.what) { case LAUNCH_ACTIVITY: { ··· handleLaunchActivity(r, null, "LAUNCH_ACTIVITY"); ··· } break; ··· } ··· } 複製代碼
ActivityThread.java
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) { ··· Activity a = performLaunchActivity(r, customIntent); ··· } private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { ··· Activity activity = null; try { //建立 Activity 對象 java.lang.ClassLoader cl = appContext.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); ··· } catch (Exception e) { ··· } try { ··· if (activity != null) { ··· 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); ··· //執行 Activity#onCreate() 生命週期 if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } ··· } ··· } catch (SuperNotCalledException e) { ··· } catch (Exception e) { ··· } 複製代碼
到這裏,整個 app 啓動流程以及源碼調用皆已分析完畢。