Activity中startActivity会执行Instrumentation.execStartActivity()的方法
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
//------省略一些代码
try {
intent.migrateExtraStreamToClipData(who);
intent.prepareToLeaveProcess(who);
int result = ActivityTaskManager.getService().startActivity(whoThread,
who.getOpPackageName(), who.getAttributionTag(), 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;
}
接下来会执行ActivityTaskManagerService的startActivity方法
@Override
public final int startActivity(IApplicationThread caller, String callingPackage,
String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,
String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,
Bundle bOptions) {
return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,
resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions,
UserHandle.getCallingUserId());
}
接下来通过ActivityStartController获取ActivityStarter执行execute()方法。然后对activity的flag和
权限做些检查,最终会调用startActivityUnchecked()->startActivityInner()。ActivityStarter 通过task startActivityLocked()
com.android.server.wm.Task#.startActivityLocked ->ensureActivitiesVisible
在执行EnsureVisibleActivitiesConfigHelper.processActivity
void process(ActivityRecord start, boolean preserveWindow) {
if (start == null || !start.mVisibleRequested) {
return;
}
reset(preserveWindow);
final PooledFunction f = PooledLambda.obtainFunction(
EnsureVisibleActivitiesConfigHelper::processActivity, this,
PooledLambda.__(ActivityRecord.class));
forAllActivities(f, start, true /*includeBoundary*/, true /*traverseTopToBottom*/);
f.recycle();
if (mUpdateConfig) {
// Ensure the resumed state of the focus activity if we updated the configuration of
// any activity.
mRootWindowContainer.resumeFocusedTasksTopActivities();
}
}
RootWindowContainer.resumeFocusedTasksTopActivities
resumeTopActivityInnerLocked -> ActivityTaskSupervisor.startSpecificActivity
-> ActivityTaskSupervisor.realStartActivityLocked
boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
boolean andResume, boolean checkConfig) throws RemoteException {
// 省略一些代码
// Create activity launch transaction.
final ClientTransaction clientTransaction = ClientTransaction.obtain(
proc.getThread(), r.appToken);
final boolean isTransitionForward = r.isTransitionForward();
clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
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, proc.getReportedProcState(),
r.getSavedState(), r.getPersistentSavedState(), results, newIntents,
r.takeOptions(), isTransitionForward,
proc.createProfilerInfoIfNeeded(), r.assistToken, activityClientController,
r.createFixedRotationAdjustmentsIfNeeded(), r.shareableActivityToken,
r.getLaunchedFromBubble()));
}
clientTransactionHandler的是实现类是ActivityThread
@Override
public void execute(ClientTransactionHandler client, IBinder token,
PendingTransactionActions pendingActions) {
Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
ActivityClientRecord r = client.getLaunchingActivity(token);
client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
}
最后在ActivityThread的hanleLaunchActivity 和 performLaunchActivity。通过mInstrumentation创建activity的实例,获取Application的实例(如果不存在的话,调用instrumentation.newApplication创建application实例),调用activity.attach方法。
hanleLaunchActivity 之后通过TransactionExecutor 执行ResumeActivityItem的execute方法。执行handleResumeActivity方法。