第①步
if (realStartActivityLocked(hr, app, true, true)) { didSomething = true; }
} catch (RemoteException e) {
Slog.w(TAG, \ + hr.intent.getComponent().flattenToShortString(), e); throw e; } } } } }
if (!didSomething) {
ensureActivitiesVisibleLocked(null, 0); }
return didSomething; }
这个方法中首先遍历进程中的堆栈,找到位于顶层的堆栈,然后调用topRunningActivityLocked() 获取位于栈顶的ActivityRecord记录,最后
调用realStartActivityLocked()方法启动activity:
★★★step17: ActivityStackSupervisor.realStartActivityLocked()
final boolean realStartActivityLocked(ActivityRecord r,
ProcessRecord app, boolean andResume, boolean checkConfig)
throws RemoteException { ...
r.app = app; ...
int idx = app.activities.indexOf(r); if (idx < 0) {
app.activities.add(r); }
final ActivityStack stack = r.task.stack;
try { ...
List
List
if (andResume) {
results = r.results;
newIntents = r.newIntents; } ... //
app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken, System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
r.compat, r.launchedFromPackage, r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results, newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo); ...
} catch (RemoteException e) { if (r.launchFailed) {
//This is the second time we failed -- finish activity and give up. ...
return false; }
// This is the first time we failed -- restart process and retry. app.activities.remove(r); throw e; } ...
return true; }
这个方法调用app.thread.scheduleLaunchActivity()真正的启动一个activity, 这里thread是IApplicationThread的实例,也就是
ActivityThread中的成员变量ApplicationThread mAppThread; 在step15的第②步初始化app时调用app.makeActive(thread, mProcessStats)
为其赋值的。 我们接着看看ApplicationThread.scheduleLaunchActivity():
step18: ApplicationThread.scheduleLaunchActivity()
@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
boolean notResumed, boolean isForward, ProfilerInfo profilerInfo) {
updateProcessState(procState, false);
ActivityClientRecord r = new ActivityClientRecord();
r.token = token; r.ident = ident; r.intent = intent; r.referrer = referrer;
r.voiceInteractor = voiceInteractor; r.activityInfo = info;
r.compatInfo = compatInfo; r.state = state;
r.persistentState = persistentState;
r.pendingResults = pendingResults; r.pendingIntents = pendingNewIntents;
r.startsNotResumed = notResumed; r.isForward = isForward;
r.profilerInfo = profilerInfo;
r.overrideConfig = overrideConfig;
updatePendingConfiguration(curConfig);
sendMessage(H.LAUNCH_ACTIVITY, r); }
ActivityThread中有一个H的成员变量,它是一个Handler, 专门接受ApplicationThread发送的消息,然后调用ActivityThread中的方法, 我
们看看H是怎样处理消息的:
step19:ActivityThread.H
public final class ActivityThread { ...
final ApplicationThread mAppThread = new ApplicationThread(); final H mH = new H();
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) { if (DEBUG_MESSAGES) Slog.v(
TAG, \ + \ Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; if (async) {
msg.setAsynchronous(true); }
mH.sendMessage(msg); }
private class H extends Handler {
public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, \handling: \+ codeToString(msg.what));
switch (msg.what) {
case LAUNCH_ACTIVITY: { //启动activity ...
handleLaunchActivity(r, null); ... }
break;
case RELAUNCH_ACTIVITY: { //重新启动activity ...
handleRelaunchActivity(r); ... }
break;
case PAUSE_ACTIVITY: //activity失去焦点 ...
handlePauseActivity((IBinder) msg.obj, false, (msg.arg1 & 1) != 0, msg.arg2,
(msg.arg1 & 2) != 0); ... break; ...
case RESUME_ACTIVITY: //activity获取焦点
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, \
handleResumeActivity((IBinder) msg.obj, true, msg.arg1 != 0, true); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); break; ... } } } }
step18中调用sendMessage(H.LAUNCH_ACTIVITY, r)之后,mH会收到一个LAUNCH_ACTIVITY消息, 然后调用了
ActivityThread.handleLaunchActivity(r, null):
step20:ActivityThread.handleLaunchActivity(r, null)
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) { ...
//创建Activity对象,并初始化,然后调用activity.attach()和onCreate() Activity a = performLaunchActivity(r, customIntent); if (a != null) {
r.createdConfig = new Configuration(mConfiguration); Bundle oldState = r.state; //调用activity.onResume()
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed);
... } else { ... } }
这里首先调用performLaunchActivity()方法创建Activity对象(调用它的attach()和onCreate()方法), 然后调用handleResumeActivity函数
来使这个Activity进入Resumed状态,并回调这个Activity的onResume函数。我们接着看看performLaunchActivity()方法中做了什么:
step21: ActivityThread.performLaunchActivity()
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { //① 收集要启动的Activity的相关信息,主要package和component