关于android:Android中Activity的工作过程一

47次阅读

共计 7561 个字符,预计需要花费 19 分钟才能阅读完成。

PS:本文系转载文章,浏览原文可读性会更好,文章开端有原文链接

PS:本文是基于 Android Api 26 来剖析源码的。Activity 作为四大组件之一,它外部工作过程零碎当然也是做了很多的封装,这种封装使得启动一个 Activity 变得异样简略。在显式调用的情景下,假如我用 MainActivity 启动 Main2Activity,那么通过如下代码即可实现;

为了更好的理解显示启动 Activity 时两个 Activity 的申明周期办法调用过程,咱们先列出 MainActivity 和 Main2Activity 的代码;(1)MainActivity 代码;

(2)Main2Activity 代码;

运行一下 app,日志打印如下所示;

看,跳转的过程中两个 Activity 的申明周期办法调用程序是这样的:MainActivity.onPause -> Main2Activity.onCreate -> Main2Activity.onStart -> Main2Activity.onResume -> MainActivity.onStop。因为下面提到的 5 个生命周期的流程波及到的源码比拟多,这里咱们先找 MainActivity 的 onPause 办法流程走向。MainActivity 启动 Main2Activity 时,它外部是怎么做到的呢?咱们看一下 Activity 的 startActivity(Intent intent) 办法的实现;

startActivity(Intent intent) 办法调用了 Activity 的 startActivity(Intent intent, @Nullable Bundle options) 办法;

因为 options 这个参数为空,所以执行的是正文 2 的代码块,咱们看一下 Activity 的 startActivityForResult(@RequiresPermission Intent intent, int requestCode) 办法;

startActivityForResult(@RequiresPermission Intent intent, int reque-stCode) 办法调用了 Activity 的 startActivityForResult(@RequiresPermi-ssion Intent intent, int requestCode,@Nullable Bundle options) 办法;

看,为什么会执行正文 3 的代码?这里首先要判断 mParent == null 是否为 true,mParent 就是以后 Activity 的父 Activity,而咱们的 Activity 是 MainActivity,它没有父类的 Activity,所以这里的 mParent == null 的后果是 true,实际上,mParent 罕用在 ActivityGroup 中,而在 API 14 开始 ActivityGroup 曾经废除。咱们看一下正文 4 的代码,mInstrumentation 是 Instrumentation 类型的对象,咱们看一下 Instrumentation 的 execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,Intent intent, int requestCode, Bundle options) 办法;

咱们先看正文 6 的代码,看这个办法名,如同是查看启动 Activity 的后果,好吧,咱们先进去看一下 Instrumentation 的 checkStartActivityResult(int res, Object intent) 办法;

外面有很多各种查看的后果,我省略了很多;看正文 7,如果咱们没有在 AndroidManifest.xml 文件外面注册 Activity,那么就会抛出 Unable to find explicit activity class  have you declared this activity in your AndroidManifest.xml? 的异样。好,咱们当初回来看正文 5 的代码这里,ActivityManager.getService() 拿到的其实是 ActivityManagerService,所以咱们往下看 ActivityManagerService 的 startActivity(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) 办法;

ActivityManagerService 的 startActivity(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) 办法又调用了 ActivityManagerService 的 startActivityAsUser 办法;

mActivityStarter 是一个 ActivityStarter 实例对象,ActivityManagerService 的 startActivityAsUser(IApplicationThread caller, String callingPackage,Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) 办法调用了 ActivityStarter 的 startActivityMayWait(IApplicationThread caller, int callingUid,String callingPackage, Intent intent, String resolvedType,IVoiceInteractionSe-ssion 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) 办法;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) {…… synchronized (mService) {…… //9、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); …… return res; }    } 看正文 9 的代码,又调用了 ActivityStarter 的 startActivityLocked(IApplicationThread caller, Intent intent, Intent ephemeralIntent,String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,IVoiceInteractionSession voiceSession, IVoi-ceInteractor 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,Activity-Record[] outActivity, ActivityStackSupervisor.ActivityContainer contain-er,TaskRecord inTask, String reason) 办法;

看正文 10 的代码,又调用了 ActivityStarter 的 startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceIn-teractor,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,Ta-skRecord inTask) 办法;

看正文 11,调用了 ActivityStarter 的 startActivity(final ActivityRecord r, ActivityRecord sourceRecord,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,ActivityRecord[] outActivity) 办法;

看正文 12,调用到了 ActivityStarter 的 startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,ActivityRec-ord[] outActivity) 办法;// Note: This method should only be called from {@link startActivity}. 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 {…… //13、mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity, mOptions); } } else {……} …… return START_SUCCESS; } 看正文 13 的代码,因为我的 Main2Activity 的启动模式是 standard,所以会走这里,mSupervisor 是 ActivityStackSupervisor 类型的实例,咱们看一下 ActivityStackSupervisor 的 resumeFocusedStackTopActivityLock-ed(ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) 办法;

看正文 14,调用了 ActivityStack 的 resumeTopActivityUncheckedLo-cked(ActivityRecord prev, ActivityOptions options) 办法;

看正文 15,调用了 ActivityStack 的 resumeTopActivityInner-Locked(ActivityRecord prev, ActivityOptions options) 办法;

看正文 16,调用了 ActivityStack 的 startPausingLocked(boolean userLeaving, boolean uiSleeping,ActivityRecord resuming, boolean pauseImmediately) 办法;

看正文 17,prev.app.thread 是 ActivityThread 的外部类 ApplicatitonThread,调用了 AppcationThread 的 schedulePauseActivity(IBinder token, boolean finished,boolean userLeaving, int configChanges, boolean dontReport) 办法;

看正文 18,因为 finished 为 false,所有第一个参数是 H.PAUSE_ACTIVITY,又调用了 ActivityThread 的外部类 H;

看正文 19,又调用了 ActivityThread 的 handlePauseActivity(IBinder token, boolean finished,boolean userLeaving, int configChanges, boolean dontReport, int seq) 办法;

看正文 20,又调用了 ActivityThread 的 performPauseActivity(IBinder token, boolean finished,boolean saveState, String reason) 办法;

看正文 21,又调用了 ActivityThread 的 performPauseActivity(Activit-yClientRecord r, boolean finished,boolean saveState, String reason) 办法;

看正文 22,又调用了 ActivityThread 的 performPauseActivity-IfNeeded(ActivityClientRecord r, String reason) 办法;

看正文 23,mInstrumentation 是 Instrumentation 类型的对象,所以调用了 Instrumentation 的 callActivityOnPause(Activity activity) 办法;

看正文 24,调用了 Activity 的 performPause 办法;

看到正文 25 了没有,Activity 的 onPause 办法曾经找到,咱们的指标曾经实现,所以这篇文章到这里就写完了。

正文完
 0