本文基于 api 28 分析
提問:從桌面點擊圖標開始 app 干了什么铛楣?
分析前先了解以下類:
Instrumentation: 在應用代碼之前實例化棘催,每個進程都有一個Instrumentation馅笙。監(jiān)控應用與所有系統(tǒng)的交互昭躺,每個 Activity 持有它的引用缀台,ActivityThread 要創(chuàng)建或暫停 Activity 時岳掐,都是通過 Instrumentation 進行操作凭疮。
ApplicationThread: 繼承 IApplicationThread.Stub,ActivityThread 的內(nèi)部類串述,是 ActivityManagerService 向 ActivityThread 通信的橋梁执解。
ActivityThread:繼承 ClientTransactionHandler ,APP的主線程纲酗,main 函數(shù)衰腌。
ActivityRecord:每個 ActivityRecord 在 AMS 中對應一個 Activity,記錄 Acitivty 信息桶唐。(一個 Activity 對應多個 ActivityRecord,因為同一個 Activity 可以被啟動多次尤泽?此條待確定)
ActivityStarter:收集 intent、task坯约、flag 等信息莫鸭,生成 ActivityRecord。
TaskRecord:任務棧被因,先進后出,存儲 ActivityRecord梨与。一個 TaskRecord 由一個或者多個 ActivityRecord 組成堕花。
ActivityStack:管理 TaskRecord,包含了多個TaskRecord粥鞋,由 ActivityStackSupervisor 創(chuàng)建缘挽。
ActivityStackSupervisor:AMS 通過 ActivityStackSupervisor 操作 ActivityStack。
ActivityManagerService:(:IActivityManager:SystemServer) 系統(tǒng)中啟動的獨立進程,負責管理四大組件壕曼,可以利用 Binder 跟它通信苏研。
ClientLifecycleManager:調(diào)用 ClientTransaction,讓 AMS 切換到 APP 進程執(zhí)行生命周期腮郊。
Activity 啟動流程涉及到進程摹蘑、線程間通信:
1、startActivity 時所在進程伴榔。
2纹蝴、AMS 進程,通過 ApplicationThread 實現(xiàn) AMS 與 ActivityThread 的進程間通信踪少。
在 ActivityManagerService 中生成 ApplicationThread 代理
ActivityThread.attach()方法:
//實例化 ApplicationThread塘安、ActivityManagerService 本地代理
final IActivityManager mgr = ActivityManager.getService();
//將 ApplicationThread 綁定在 ActivityManagerService(通過 ProcessRecord) 中
mgr.attachApplication(mAppThread, startSeq);
mgr.attachApplicationLocked(thread)
new ProcessRecord().makeActive(thread) 將 applicationThread 綁定在 ProcessRecord 中
activityManagerServiec attach applicationThread
3、目標 Activity 進程援奢,ActivityThread 中 Handler 線程間通信兼犯。
圖片來源:基于Android-28 的源碼 Activity 啟動流程分析
第一階段:startActivity
1.1 第一種啟動方式,桌面圖標啟動 startActivity集漾,實現(xiàn)類是 ContextImpl切黔。
class ContextImpl extends Context {
public void startActivity(Intent intent) {
...
startActivity(intent, null);
}
@Override
public void startActivity(Intent intent, Bundle options) {
warnIfCallingFromSystemProcess();
...
//mMainThread 是 ActivityThread。mMainThread.getApplicationThread() 是獲取 ActivityThread 的內(nèi)部類ApplicationThread,它是一個 IBinder具篇,主要用于 ActivityThread 與 AMS 的通信纬霞。
mMainThread.getInstrumentation().execStartActivity(
getOuterContext(), mMainThread.getApplicationThread(), null,
(Activity) null, intent, -1, options);
}
}
1.2 第二種啟動方式,應用內(nèi)打開 activity驱显,實現(xiàn)類 Activity.
public class Activity extends ContextThemeWrapper{
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
@Nullable Bundle options) {
...
mInstrumentation.execStartActivity(
this, mMainThread.getApplicationThread(), mToken, this,
intent, requestCode, options);
...
}
由此可見 ContextImpl 與 Activity 最終都是通過 Instrumentation 的 execStartActivity() 啟動 Activity诗芜。
public class Instrumentation {
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
IApplicationThread whoThread = (IApplicationThread) contextThread;
...
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);
...
}
}
public class ActivityManager {
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;
}
};
}
ActivityManager.getService() 獲取得到的是 IActivityManager 的接口伏恐,是 ActivityManagerService(AMS) 遠程代理類翠桦。AMS 運行在另外一個進程销凑,繼承了 IBinder斗幼,是 binder 通信的 Server 端孟岛。通過 IActivityManager 就可以跟 AMS 實現(xiàn) Binder 通信督勺。
第二階段:AMS 進程
public class ActivityManagerService extends IActivityManager.Stub {
@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());
}
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,
boolean validateIncomingUser) {
//mActivityStartController.obtainStarter 獲取到 ActivityStarter
return mActivityStartController.obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setResolvedType(resolvedType)
.setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setMayWait(userId)
.execute();
}
}
ActivityStarter 收集 intent次询、選擇啟動的 task屯吊、flag 等信息盒卸,復用Activity等邏輯蔽介,生成 ActivityRecord虹蓄。
class ActivityStarter {
int execute() {
...
startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent,
mRequest.resolvedType, mRequest.activityInfo, mRequest.resolveInfo,
mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
mRequest.resultWho, mRequest.requestCode, mRequest.callingPid,
mRequest.callingUid, mRequest.callingPackage, mRequest.realCallingPid,
mRequest.realCallingUid, mRequest.startFlags, mRequest.activityOptions,
mRequest.ignoreTargetSecurity, mRequest.componentSpecified,
mRequest.outActivity, mRequest.inTask, mRequest.reason,
mRequest.allowPendingRemoteAnimationRegistryLookup);
}
}
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,
SafeActivityOptions options,
boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup) {
...
//構(gòu)造當前的 Actiivty 的 ActivityRecord
ActivityRecord r = new ActivityRecord(mService, callerApp, callingPid, callingUid,
callingPackage, intent, resolvedType, aInfo, mService.getGlobalConfiguration(),
resultRecord, resultWho, requestCode, componentSpecified, voiceSession != null,
mSupervisor, checkedOptions, sourceRecord);
...
return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,
true /* doResume */, checkedOptions, inTask, outActivity);
}
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity) {
...
result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, outActivity);
...
return result;
}
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity) {
...
//mSupervisor 是 ActivityStackSupervisor
mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
mOptions);
}
...
}
}
ActivityStarter 構(gòu)造了ActivityRecord,選擇ActivityStack等等律胀。
startActivityUnchecked 做了決定任務棧累铅,根據(jù)啟動模式娃兽,是否調(diào)用deliverNewIntent投储,復用Activity 等動作阔馋。
public class ActivityStackSupervisor extends ConfigurationContainer {
boolean resumeFocusedStackTopActivityLocked(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {
...
//targetStack 是 ActivityStack
return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
...
}
}
class ActivityStack<T extends StackWindowController> extends ConfigurationContainer {
boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
...//1
result = resumeTopActivityInnerLocked(prev, options);
...
}
@GuardedBy("mService")
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
...//2
if (mResumedActivity != null) {
//構(gòu)造 PauseActivityItem 執(zhí)行 pause 方法勋眯,暫停當前的 Activity
pausing |= startPausingLocked(userLeaving, false, next, false);
}
if (pausing && !resumeWhilePausing) {
...
return true;
}
...
if (next.app != null && next.app.thread != null) {
...
//如果 Activity 已存在客蹋,執(zhí)行 resume
mStackSupervisor.scheduleResumeTopActivities();
} else {
...//3
//創(chuàng)建進程讶坯,冷啟動并創(chuàng)建 Activity∈欤或者已啟動 App娩井,重新啟動 Activity
mStackSupervisor.startSpecificActivityLocked(next, true, true);
}
}
resumeTopActivityInnerLocked() 主要負責暫停當前 Activity 和啟動新的 Activity撞牢。
先來看 startPausingLocked 暫停 Activity 方法叔营,再看 startSpecificActivityLocked 啟動 Activity 方法
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
ActivityRecord resuming, boolean pauseImmediately) {
...
//mService.getLifecycleManager() 是獲取到 ClientLifecycleManager绒尊, 這個類負責執(zhí)行 ActivityLifecycleItem婴谱,就是生命周期的任務谭羔,如 PauseActivityItem 瘟裸、ResumeActivtyItem
mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,
PauseActivityItem.obtain(prev.finishing, userLeaving,
prev.configChangeFlags, pauseImmediately));
}
}
public class ActivityStackSupervisor extends ConfigurationContainer {
void startSpecificActivityLocked(ActivityRecord r,
boolean andResume, boolean checkConfig) {
...
//如果 app 進程存在
if (app != null && app.thread != null) {
realStartActivityLocked(r, app, andResume, checkConfig);
return;
}
//如果 app 進程不存在
mService.startProcessLocked(r.processName, r.info.applicationInfo, true, 0,
"activity", r.intent.getComponent(), false, false, true); ...
}
final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app,
boolean andResume, boolean checkConfig) throws RemoteException {
...
final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
r.appToken);
clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
System.identityHashCode(r), r.info,
mergedConfiguration.getGlobalConfiguration(),
mergedConfiguration.getOverrideConfiguration(), r.compat,
r.launchedFromPackage, task.voiceInteractor, app.repProcState, r.icicle,
r.persistentState, results, newIntents, mService.isNextTransitionForward(),
profilerInfo));
final ActivityLifecycleItem lifecycleItem;
if (andResume) {//andResume 為 true
lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
} else {
lifecycleItem = PauseActivityItem.obtain();
}
//lifecycleItem 是 ResumeActivtyItem
clientTransaction.setLifecycleStateRequest(lifecycleItem);
//mService.getLifecycleManager() ClientLifecycleManager
mService.getLifecycleManager().scheduleTransaction(clientTransaction);
...
}
}
這里分兩種情況兼搏,一是 app 進程存在執(zhí)行 realeStartActivityLocked() 佛呻,另一個是 app 進程不存在吓著,執(zhí)行 AMS 的 startProcessLocked() ,Zygote 進程 fork 子進程泛范,啟動進程執(zhí)行 ActivityThread 的 main 方法紊撕。
class ClientLifecycleManager {
void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
final IApplicationThread client = transaction.getClient();
//執(zhí)行 ClientTransaction 的 schedule() 方法
transaction.schedule();
...
}
}
public class ClientTransaction {
public void schedule() throws RemoteException {
//mClient 為 IApplicationThread对扶,實際是 ActivityThread 中的 ApplicationThread
mClient.scheduleTransaction(this);
}
}
ClientTransaction 中包裝了 ActivityLifecycleItem浪南,就是生命周期的任務络凿,如 PauseActivityItem 絮记、ResumeActivtyItem怨愤。
IApplicationThread 是 ActivityThread 內(nèi)部類 ApplicationThread撰洗,是 Binder 對象腐芍,IApplicationThread 是它遠程接口猪勇。
當執(zhí)行 ApplicationThread. scheduleTransaction() 會從 AMS 進程回到目標 Activity 進程泣刹。
接下來看 ApplicationThread
目標 Activity 進程,ActivityThread 中 Handler 線程間通信貌笨。
public final class ActivityThread extends ClientTransactionHandler {
private class ApplicationThread extends IApplicationThread.Stub {
//ApplicationThread 是 Binder 對象锥惋,IApplicationThread 是它遠程接口。
@Override
public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
//1
ActivityThread.this.scheduleTransaction(transaction);
}
}
//ActivityThread 繼承自 ClientTransactionHandler遭商,scheduleTransaction 是 ClientTransactionHandler 中方法
void scheduleTransaction(ClientTransaction transaction) {
transaction.preExecute(this);
//2 發(fā)送消息 EXECUTE_TRANSACTION
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
...
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
msg.arg1 = arg1;
msg.arg2 = arg2;
if (async) {
msg.setAsynchronous(true);
}
//3劫流,mH 是 Handler,將異步 msg 發(fā)送給 Handler 同步處理
mH.sendMessage(msg);
}
class H extends Handler {
...
//回到主線程可很,處理 EXECUTE_TRANSACTION 消息
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
//4我抠、mTransactionExecutor 是 TransactionExecutor
mTransactionExecutor.execute(transaction);
break;
}
}
}
}
public class TransactionExecutor {
public void execute(ClientTransaction transaction) {
...
//5菜拓、
executeCallbacks(transaction);
//6尘惧、
executeLifecycleState(transaction);
}
@VisibleForTesting
public void executeCallbacks(ClientTransaction transaction) {
final List<ClientTransactionItem> callbacks = transaction.getCallbacks();
...
final int size = callbacks.size();
for (int i = 0; i < size; ++i) {
final ClientTransactionItem item = callbacks.get(i);
...
//5.1
if (closestPreExecutionState != UNDEFINED) {
//預初始化狀態(tài) ON_CREATE/ON_START 等
cycleToPath(r, closestPreExecutionState);
}
//5.2 item 為 PauseActivityItem/ResumeActivityItem 等 ClientTransactionItem 類
item.execute(mTransactionHandler, token, mPendingActions);
//5.3
item.postExecute(mTransactionHandler, token, mPendingActions);
}
}
private void cycleToPath(ActivityClientRecord r, int finish,
boolean excludeLastState) {
...
performLifecycleSequence(r, path);
}
/** mTransactionHandler 是 ActivityThread喷橙,最終調(diào)用 ActivityThread 方法 */
private void performLifecycleSequence(ActivityClientRecord r, IntArray path) {
final int size = path.size();
for (int i = 0, state; i < size; i++) {
state = path.get(i);
log("Transitioning to state: " + state);
switch (state) {
case ON_CREATE:
mTransactionHandler.handleLaunchActivity(r, mPendingActions,
null /* customIntent */);
break;
case ON_START:
mTransactionHandler.handleStartActivity(r, mPendingActions);
break;
case ON_RESUME:
mTransactionHandler.handleResumeActivity(r.token, false /* finalStateRequest */,
r.isForward, "LIFECYCLER_RESUME_ACTIVITY");
break;
case ON_PAUSE:
mTransactionHandler.handlePauseActivity(r.token, false /* finished */,
false /* userLeaving */, 0 /* configChanges */, mPendingActions,
"LIFECYCLER_PAUSE_ACTIVITY");
break;
case ON_STOP:
mTransactionHandler.handleStopActivity(r.token, false /* show */,
0 /* configChanges */, mPendingActions, false /* finalStateRequest */,
"LIFECYCLER_STOP_ACTIVITY");
break;
case ON_DESTROY:
mTransactionHandler.handleDestroyActivity(r.token, false /* finishing */,
0 /* configChanges */, false /* getNonConfigInstance */,
"performLifecycleSequence. cycling to:" + path.get(size - 1));
break;
case ON_RESTART:
mTransactionHandler.performRestartActivity(r.token, false /* start */);
break;
default:
throw new IllegalArgumentException("Unexpected lifecycle state: " + state);
}
}
}
}
至此贰逾,這一段講了 ActivityLifecycleItem 通過 ClientLifecycleManager 走到 ActivityThread疙剑,并切換到主線程執(zhí)行 execute()
參考:
基于Android-28 的源碼 Activity 啟動流程分析
ActivityRecord言缤、TaskRecord管挟、ActivityStack以及Activity啟動模式詳解
Activity生命周期回調(diào)是如何被回調(diào)的?
ActivityRecord僻孝、TaskRecord、ActivityStack以及Activity啟動模式詳解