一、准备好阅读源码的工具。html
二、主要类的功能介绍java
咱们从源码的package android.app.Activity.java 的 startActivity 方法分析,其中 startActivity 方法有几种重载方式:android
@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 { startActivityForResult(intent, -1); } }
两个重载方法,都调用了 startActivityForResult 方法,代码以下:设计模式
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,@Nullable Bundle options) { if (mParent == null) { // mParent 表示这 当如今的Activity 没有父Activity options = transferSpringboardActivityOptions(options); Instrumentation.ActivityResult ar = mInstrumentation.execStartActivity( this, mMainThread.getApplicationThread(), mToken, this, intent, requestCode, options); if (ar != null) { mMainThread.sendActivityResult( mToken, mEmbeddedID, requestCode, ar.getResultCode(), ar.getResultData()); } if (requestCode >= 0) { mStartedActivity = true; } cancelInputsAndStartExitTransition(options); } .... ignore some code ... }
在前面说过Instrumentation这个类,每一个Activity都持有一个Instrumentation对象的一个引用,当时整个进程中只会存在一个Instrumentation对象,当startActivityForResult方法调用后实际仍是调用了mInstrumentation.execStartActivity,mInstrumentation 在Activity的attach方法中初始化。服务器
这里注意 mMainThread 是 ActivityThread 也就是咱们常说的主线程,mMainThread.getApplicationThread() ApplicationThread 是ActivityThread 的内部类。app
当 mParent != null 时走下面这段代码:async
else { if (options != null) { //当如今的Activity有父Activity的时候会调用,可是在startActivityFromChild()内部实际仍是调用的mInstrumentation.execStartActivity() mParent.startActivityFromChild(this, intent, requestCode, options); } else { mParent.startActivityFromChild(this, intent, requestCode); } }
Instrumentation 的 execStartActivity 方法,代码实现以下:ide
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,Intent intent, int requestCode, Bundle options) { ... ignore some code ... 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; }
因此,当咱们程序调用startActivity方法的时候,其实是调用的Instrumentation 的相关方法。函数
Instrumentation 能够意为‘仪器’,咱们看一下这个类大体包含哪些方法工具
[图片上传失败...(image-118e35-1522472148194)]
这个类里面的方法大多数和Application和Activity 有关,这个类就是完成了对Application和Activity的初始化和生命周期的工具类,好比咱们看一下callActivityOnCreate方法,代码以下:
/** * Perform calling of an activity's {@link Activity#onCreate} 注意这一句话,调用了Activity的入口函数 onCreate * method. The default implementation simply calls through to that method. * * @param activity The activity being created. * @param icicle The previously frozen state (or null) to pass through to onCreate(). */ public void callActivityOnCreate(Activity activity, Bundle icicle) { prePerformCreate(activity); activity.performCreate(icicle); postPerformCreate(activity); }
再看一下activity.performCreate(icicle);方法
final void performCreate(Bundle icicle) { restoreHasCurrentPermissionRequest(icicle); onCreate(icicle); mActivityTransitionState.readState(icicle); performCreateCommon(); }
Instrumentation 这个类是很重要的,对Activity的生命周期方法调用根本离不开他,这么重要的类,为何在开发中没有发现他的轨迹呢?
Instrumentation 能够说是一个大管家,管内无论外,他是老板娘。
老板是谁呢?固然是 ActivityThread .
AMS说:“ActivityThread,你给我暂停一个Activity!”
ActivityThread就说:“没问题!”而后转身和Instrumentation说:“老婆,AMS让暂停一个Activity,我这里忙着呢,你快去帮我把这事办了把~”
因而,Instrumentation就去把事儿搞定了。
因此说,AMS是董事会,负责指挥和调度的,ActivityThread是老板,虽说家里的事本身说了算,可是须要遵从AMS的指挥,而Instrumentation则是老板娘,负责家里的大事小事,可是通常不抛头露面,听一家之主ActivityThread的安排。
咱们继续分析 execStartActivity方法,根据这段代码能够看出,启动Activity 是由 ActivityManager.getService().startActivity 方法启动的,这里注意API 26 及26 以后源码有变更。
int result = ActivityManager.getService().startActivity(whoThread, who.getBasePackageName(), intent,intent.resolveTypeIfNeeded(who.getContentResolver()),token, target != null ? target.mEmbeddedID : null,requestCode, 0, null, options);
ActivityManager.getService() 的代码以下:
/** * @hide */ 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; } };
IActivityManager.Stub.asInterface(b); 这句代码我找了很久终于在 ActivityManagerNative 类中找到了 代码以下:
public abstract class ActivityManagerNative { /** * Cast a Binder object into an activity manager interface, generating * a proxy if needed. * * @deprecated use IActivityManager.Stub.asInterface instead. // 最重要的是这句话 改成使用IActivityManager.Stub.asInterface */ static public IActivityManager asInterface(IBinder obj) { return IActivityManager.Stub.asInterface(obj); } static public IActivityManager getDefault() { return ActivityManager.getService(); } }
咱们在看一下 API 26 以前的ActivityManagerNative源码是如何写的呢?代码以下:
public abstract class ActivityManagerNative extends Binder implements IActivityManager { /** * //最终返回的仍是一个ActivityManagerProxy对象 */ static public IActivityManager asInterface(IBinder obj) { if (obj == null) { return null; } IActivityManager in = (IActivityManager)obj.queryLocalInterface(descriptor); if (in != null) { return in; } //这里面的Binder类型的obj参数会做为ActivityManagerProxy的成员变量保存为mRemote成员变量,负责进行IPC通讯 return new ActivityManagerProxy(obj); } /** * //从类声明上,咱们能够看到ActivityManagerNative是Binder的一个子类,并且实现了IActivityManager接口 */ static public IActivityManager getDefault() { return gDefault.get(); } //经过单例模式获取一个IActivityManager对象,这个对象经过asInterface(b)得到 private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() { protected IActivityManager create() { IBinder b = ServiceManager.getService("activity"); if (false) { Log.v("ActivityManager", "default service binder = " + b); } IActivityManager am = asInterface(b); if (false) { Log.v("ActivityManager", "default service = " + am); } return am; } }; } ...ignore some code ... }
搜得死内 ,asInterface 方法返回的就是ActivityManagerService的远程接口,即ActivityManagerProxy。
咱们再看一下 ActivityManagerProxy 方法中的 startActivity() 方法,这里面主要作的事情就是IPC通讯,利用 Binder 对象,调用transact(),把全部须要的参数封装成 Parcel对象,向AMS发送数据通讯,代码以下:
public int startActivity(IApplicationThread caller, String callingPackage, Intent intent,String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle options) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); data.writeStrongBinder(caller != null ? caller.asBinder() : null); data.writeString(callingPackage); intent.writeToParcel(data, 0); data.writeString(resolvedType); data.writeStrongBinder(resultTo); data.writeString(resultWho); data.writeInt(requestCode); data.writeInt(startFlags); if (profilerInfo != null) { data.writeInt(1); profilerInfo.writeToParcel(data, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); } else { data.writeInt(0); } if (options != null) { data.writeInt(1); options.writeToParcel(data, 0); } else { data.writeInt(0); } mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0); reply.readException(); int result = reply.readInt(); reply.recycle(); data.recycle(); return result; }
private IBinder mRemote;
mRemote.transact(START_ACTIVITY_TRANSACTION, data, reply, 0);
Binder本质上只是一种底层通讯方式,和具体服务没有关系。为了提供具体服务,Server必须提供一套接口函数以便Client经过远程访问使用各类服务。这时一般采用Proxy设计模式:将接口函数定义在一个抽象类中,Server和Client都会以该抽象类为基类实现全部接口函数,所不一样的是Server端是真正的功能实现,而Client端是对这些函数远程调用请求的包装。
具体的流程是这样的: 客户端:ActivityManagerProxy =====>Binder驱动=====> ActivityManagerService:服务器
可是!这里Binder通讯是单方向的,即从ActivityManagerProxy指向ActivityManagerService的,若是AMS想要通知ActivityThread作一些事情,应该咋办呢?
仍是经过Binder通讯,不过是换了另一对,换成了ApplicationThread和ApplicationThreadProxy。
客户端:ApplicationThread <=====Binder驱动<===== ApplicationThreadProxy:服务器
能够看出不论是API 26 仍是 API 26 以前的 ,主要作的事情就是IPC通讯,利用 Binder 对象,向AMS发送数据通讯。
实际上就是调用了AMS 中的startActivity方法。
public final class ActivityManagerService extends ActivityManagerNative implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback
API 26 AMS 的继承关系也发生了改变
public class ActivityManagerService extends IActivityManager.Stub implements Watchdog.Monitor, BatteryStatsImpl.BatteryCallback
两种一对比其实实现都是同样的逻辑。
OK,当咱们点击桌面图标调用startActivity,终于把数据和要开启的Activity的请求发送到了AMS,那么AMS到底作了什么呢?
注意!这一块调用的方法链很是之多,若是头脑不够清晰建议休息一下。
AMS收到startActivity的请求以后,会按照以下的方法链进行调用
调用startActivity()
@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()); }
调用startActivityAsUser()
@Override public final int startActivityAsUser(IApplicationThread caller, String callingPackage, Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle options, int userId) { ...ignore some code... return mStackSupervisor.startActivityMayWait(caller, -1, callingPackage, intent, resolvedType, null, null, resultTo, resultWho, requestCode, startFlags, profilerInfo, null, null, options, userId, null, null); }
在这里又出现了一个新对象ActivityStackSupervisor,经过这个类能够实现对ActivityStack的部分操做。
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 config, Bundle options, int userId, IActivityContainer iContainer, TaskRecord inTask) { ...ignore some code... int res = startActivityLocked(caller, intent, resolvedType, aInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options, componentSpecified, null, container, inTask); ...ignore some code... }
继续调用startActivityLocked()
final int startActivityLocked(IApplicationThread caller, Intent intent, String resolvedType, ActivityInfo aInfo, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid, String callingPackage, int realCallingPid, int realCallingUid, int startFlags, Bundle options, boolean componentSpecified, ActivityRecord[] outActivity, ActivityContainer container, TaskRecord inTask) { err = startActivityUncheckedLocked(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true, options, inTask); if (err < 0) { notifyActivityDrawnForKeyguard(); } return err; }
调用startActivityUncheckedLocked(),此时要启动的Activity已经经过检验,被认为是一个正当的启动请求。
终于,在这里调用到了ActivityStack的startActivityLocked(ActivityRecord r, boolean newTask,boolean doResume, boolean keepCurTransition, Bundle options)。
ActivityRecord表明的就是要开启的Activity对象,里面分装了不少信息,好比所在的ActivityTask等,若是这是首次打开应用,那么这个Activity会被放到ActivityTask的栈顶,
final int startActivityUncheckedLocked(ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags, boolean doResume, Bundle options, TaskRecord inTask) { ...ignore some code... targetStack.startActivityLocked(r, newTask, doResume, keepCurTransition, options); ...ignore some code... return ActivityManager.START_SUCCESS; }
调用的是ActivityStack.startActivityLocked()
final void startActivityLocked(ActivityRecord r, boolean newTask, boolean doResume, boolean keepCurTransition, Bundle options) { //ActivityRecord中存储的TaskRecord信息 TaskRecord rTask = r.task; ...ignore some code... //若是不是在新的ActivityTask(也就是TaskRecord)中的话,就找出要运行在的TaskRecord对象 TaskRecord task = null; if (!newTask) { boolean startIt = true; for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) { task = mTaskHistory.get(taskNdx); if (task.getTopActivity() == null) { // task中的全部Activity都结束了 continue; } if (task == r.task) { // 找到了 if (!startIt) { task.addActivityToTop(r); r.putInHistory(); mWindowManager.addAppToken(task.mActivities.indexOf(r), r.appToken, r.task.taskId, mStackId, r.info.screenOrientation, r.fullscreen, (r.info.flags & ActivityInfo.FLAG_SHOW_ON_LOCK_SCREEN) != 0, r.userId, r.info.configChanges, task.voiceSession != null, r.mLaunchTaskBehind); if (VALIDATE_TOKENS) { validateAppTokensLocked(); } ActivityOptions.abort(options); return; } break; } else if (task.numFullscreen > 0) { startIt = false; } } } ...ignore some code... // Place a new activity at top of stack, so it is next to interact // with the user. task = r.task; task.addActivityToTop(r); task.setFrontOfTask(); ...ignore some code... if (doResume) { mStackSupervisor.resumeTopActivitiesLocked(this, r, options); } }
从ActivityStackSupervisor到ActivityStack,又调回ActivityStackSupervisor,!!!!!!!!!!!!!!!!!
淡定…淡定…我知道你也在内心骂娘,世界如此美妙,你却如此暴躁,这样很差,很差…
继续,刚才说到哪里了?我们一块儿看下StackSupervisor.resumeTopActivitiesLocked(this, r, options)
boolean resumeTopActivitiesLocked(ActivityStack targetStack, ActivityRecord target, Bundle targetOptions) { if (targetStack == null) { targetStack = getFocusedStack(); } // Do targetStack first. boolean result = false; if (isFrontStack(targetStack)) { result = targetStack.resumeTopActivityLocked(target, targetOptions); } ...ignore some code... return result; }
又调回ActivityStack去了…
ActivityStack.resumeTopActivityLocked()
final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) { if (inResumeTopActivity) { // Don't even start recursing. return false; } boolean result = false; try { // Protect against recursion. inResumeTopActivity = true; result = resumeTopActivityInnerLocked(prev, options); } finally { inResumeTopActivity = false; } return result; }
我们坚持住,看一下ActivityStack.resumeTopActivityInnerLocked()到底进行了什么操做
final boolean resumeTopActivityInnerLocked(ActivityRecord prev, Bundle options) { ...ignore some code... //找出还没结束的首个ActivityRecord ActivityRecord next = topRunningActivityLocked(null); //若是一个没结束的Activity都没有,就开启Launcher程序 if (next == null) { ActivityOptions.abort(options); if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: No more activities go home"); if (DEBUG_STACK) mStackSupervisor.validateTopActivitiesLocked(); // Only resume home if on home display final int returnTaskType = prevTask == null || !prevTask.isOverHomeStack() ? HOME_ACTIVITY_TYPE : prevTask.getTaskToReturnTo(); return isOnHomeDisplay() &&mStackSupervisor.resumeHomeStackTask(returnTaskType, prev); } //先须要暂停当前的Activity。由于咱们是在Lancher中启动mainActivity,因此当前mResumedActivity!=null,调用startPausingLocked()使得Launcher进入Pausing状态 if (mResumedActivity != null) { pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause); if (DEBUG_STATES) Slog.d(TAG, "resumeTopActivityLocked: Pausing " + mResumedActivity); } }
在这个方法里,prev.app为记录启动Lancher进程的ProcessRecord,prev.app.thread为Lancher进程的远程调用接口IApplicationThead,因此能够调用prev.app.thread.schedulePauseActivity,到Lancher进程暂停指定Activity。
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming, boolean dontWait) { if (mPausingActivity != null) { completePauseLocked(false); } ...ignore some code... if (prev.app != null && prev.app.thread != null) try { mService.updateUsageStats(prev, false); prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing, userLeaving, prev.configChangeFlags, dontWait); } catch (Exception e) { mPausingActivity = null; mLastPausedActivity = null; mLastNoHistoryActivity = null; } } else { mPausingActivity = null; mLastPausedActivity = null; mLastNoHistoryActivity = null; } ...ignore some code... }
在Lancher进程中消息传递,调用ActivityThread.handlePauseActivity(),最终调用ActivityThread.performPauseActivity()暂停指定Activity。接着经过前面所说的Binder通讯,通知AMS已经完成暂停的操做。
ActivityManagerNative.getDefault().activityPaused(token).
上面这些调用过程很是复杂,源码中各类条件判断让人眼花缭乱,因此说若是你没记住也不要紧,你只要记住这个流程,理解了Android在控制Activity生命周期时是如何操做,以及是经过哪几个关键的类进行操做的就能够了,之后遇到相关的问题之道从哪块下手便可,这些过程我虽然也是撸了一遍,但仍是记不清。
须要注意的代码 app.thread.scheduleLaunchActivity 这个方法
final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app, boolean andResume, boolean checkConfig) throws RemoteException { .................... 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);. .................. }
app.thread 实际上就是ApplicationThread,最终回到了ApplicationThread 的scheduleLaunchActivity 方法 来启动Activity,咱们知道ApplicationThread 是ActivityThread 的内部类
private class ApplicationThread extends IApplicationThread.Stub { @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) { 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); } }
scheduleLaunchActivity 实现方法很简单 发送了一个启动Activity的消息交给Handler处理。
sendMessage 代码以下:
private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) { if (DEBUG_MESSAGES) Slog.v( TAG, "SCHEDULE " + what + " " + mH.codeToString(what) + ": " + arg1 + " / " + obj); Message msg = Message.obtain(); msg.what = what; msg.obj = obj; msg.arg1 = arg1; msg.arg2 = arg2; if (async) { msg.setAsynchronous(true); } mH.sendMessage(msg); }
接下来看一下 Handler H 对消息的处理
public void handleMessage(Message msg) { if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what)); switch (msg.what) { case LAUNCH_ACTIVITY: { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart"); final ActivityClientRecord r = (ActivityClientRecord) msg.obj; r.packageInfo = getPackageInfoNoCheck( r.activityInfo.applicationInfo, r.compatInfo); handleLaunchActivity(r, null, "LAUNCH_ACTIVITY"); Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); } break; }
能够看出Activity的启动过程由 ActivityThread handleLaunchActivity 方法实现的,看一下 handleLaunchActivity 的代码以下:
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent, String reason) { // If we are getting ready to gc after going to the background, well // we are back active so skip it. unscheduleGcIdler(); mSomeActivitiesChanged = true; if (r.profilerInfo != null) { mProfiler.setProfiler(r.profilerInfo); mProfiler.startProfiling(); } // Make sure we are running with the most recent config. handleConfigurationChanged(null, null); if (localLOGV) Slog.v( TAG, "Handling launch of " + r); // Initialize before creating the activity WindowManagerGlobal.initialize(); Activity a = performLaunchActivity(r, customIntent); if (a != null) { r.createdConfig = new Configuration(mConfiguration); reportSizeConfigurations(r); Bundle oldState = r.state; handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason); if (!r.activity.mFinished && r.startsNotResumed) { performPauseActivityIfNeeded(r, reason); if (r.isPreHoneycomb()) { r.state = oldState; } } } else { // If there was an error, for any reason, tell the activity manager to stop us. try { ActivityManager.getService() .finishActivity(r.token, Activity.RESULT_CANCELED, null, Activity.DONT_FINISH_TASK_WITH_ACTIVITY); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } }
从上面代码能够看出,performLaunchActivity 方法最终完成了Activity对象的建立和启动过程,而且经过handleLaunchActivity 方法的handleResumeActivity 调用被启动Activity的onResume 这一个生命周期方法。
简单看一下 performLaunchActivity 的代码,作了些什么:
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) { //1 从ActivityClientRecord 中获取待启动 Activity的组件信息 ActivityInfo aInfo = r.activityInfo; if (r.packageInfo == null) { r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo, Context.CONTEXT_INCLUDE_CODE); } ComponentName component = r.intent.getComponent(); if (component == null) { component = r.intent.resolveActivity( mInitialApplication.getPackageManager()); r.intent.setComponent(component); } if (r.activityInfo.targetActivity != null) { component = new ComponentName(r.activityInfo.packageName, r.activityInfo.targetActivity); } // 2 经过Instrumentation的newActivity 方法使用类加载器建立Activity对象 ContextImpl appContext = createBaseContextForActivity(r); Activity activity = null; try { java.lang.ClassLoader cl = appContext.getClassLoader(); activity = mInstrumentation.newActivity( cl, component.getClassName(), r.intent); StrictMode.incrementExpectedActivityCount(activity.getClass()); r.intent.setExtrasClassLoader(cl); r.intent.prepareToEnterProcess(); if (r.state != null) { r.state.setClassLoader(cl); } } catch (Exception e) { if (!mInstrumentation.onException(activity, e)) { throw new RuntimeException( "Unable to instantiate activity " + component + ": " + e.toString(), e); } } //3 经过LoadedApk 的 makeApplication 方法来尝试建立 Application 对象 Application app = r.packageInfo.makeApplication(false, mInstrumentation); ........................... appContext.setOuterContext(activity); // 4 建立ContextImpl 对象并经过Activity的attach方法来完成一些重要数据的初始化 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); ......................... // 5 调用Activity的生命周期方法 activity.mCalled = false; if (r.isPersistable()) { mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState); } else { mInstrumentation.callActivityOnCreate(activity, r.state); } .................................. return activity; }
至此,整个Activity的启动过程完成。