Activity启动流程

1、boolean onKeyDown(intkey Code,KeyEvent event)(Activity.java)  ——》

 2、void startActivity(Intent intent)(Activity.java)  ——》 

3、void startActivity(Intent intent,@Nullable Bundle options)(Activity.java)  ——》 

4、void startActivityForResult(@RequiresPermission Intent intent, intrequestCode,

@NullableBundle options)(Activity.java)  ——》

5、ActivityResult execStartActivity(

Context who,IBinder contextThread,IBinder token,Activity target,

Intent intent, intrequestCode,Bundle options)

(Instrumentation.java)  ——》

6、IActivityManager getDefault()(AMN) ——》

7、int startActivity(IApplicationThread caller,String callingPackage,Intent intent,

String resolvedType,IBinder resultTo,String resultWho, intrequestCode, intflags,

ProfilerInfo profilerInfo,Bundle options)throwsRemoteException(AMP,AMP是AMN的内部类,实现IActivityManager类) (AMP经过binder IPC,进入ActivityManagerNative(简称AMN)) ——》

8、boolean onTransact(int code,Parcel data,Parcel reply, int flags)

throwsRemoteException(AMN)  ——》(根据不同code情况,分别进入AMS不同的启动Activity方法)(不知道为什么会进入AMS的方法,应该是代码更改,将ActivityManagerProxy作为AMN内部类,从而代码依然是在AMN中)

9、int startActivity(IApplicationThread caller,StringcallingPackage,

Intent intent,StringresolvedType,IBinder resultTo,StringresultWho, intrequestCode,

intstartFlags,ProfilerInfo profilerInfo,Bundle bOptions)(AMN)  ——》(不知道为什么,又进入了ActivityStackSupervisor而不是ActivityStarter,应该是系统代码更改,进入的是后者)

10、int startActivityMayWait(IApplicationThread caller,intcallingUid,        String callingPackage, Intent intent, String resolvedType,        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,        IBinder resultTo, String resultWho,intrequestCode,intstartFlags,        ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,        Bundle options,booleanignoreTargetSecurity,intuserId,        IActivityContainer iContainer, TaskRecord inTask)(ActivityStarter.java) 

(ResolveInforInfo =mSupervisor.resolveIntent(intent,resolvedType,userId);

ActivityInfo aInfo =mSupervisor.resolveActivity(intent,rInfo,startFlags,profilerInfo);

(ASS.java resolveActivity()方法的核心功能是找到相应的Activity组件,并保存到intent对象)

通过resolveActivity来获取ActivityInfo信息

然后再进入AS.startActivityLocked()

) ——》

11、ActivityInforesolveActivity(Intent intent,ResolveInfo rInfo, intstartFlags,

ProfilerInfo profilerInfo)(ASS) 

决定启动模式:

ActivityManager类有如下4个flags用于调试:

START_FLAG_DEBUG:用于调试debug app

START_FLAG_OPENGL_TRACES:用于调试OpenGL tracing

START_FLAG_NATIVE_DEBUGGING:用于调试native

START_FLAG_TRACK_ALLOCATION: 用于调试allocation tracking

 ——》

12、步骤10中resolveIntent(intent,resolvedType,userId)方法:

AppGlobals.getPackageManager().
resolveIntent(Intent intent,String resolvedType,

intflags, intuserId)(PMS)——》

13、List queryIntentActivitiesInternal(Intent intent,

String resolvedType, int flags, int userId)

(PMS,遍历activity)——》

14、ResolveInfo chooseBestActivity(Intent intent,String resolvedType,

intflags,List query, intuserId)(PMS.java 选择activity)——》

15、步骤10中进入

int startActivityLocked(IApplicationThread caller,Intent intent,Intent ephemeralIntent,

String resolvedType,ActivityInfo aInfo,ResolveInfo rInfo,

IVoiceInteractionSession voiceSession,IVoiceInteractor voiceInteractor,

IBinder resultTo,String resultWho, intrequestCode, intcallingPid, intcallingUid,

String callingPackage, intrealCallingPid, intrealCallingUid, intstartFlags,

ActivityOptions options, booleanignoreTargetSecurity, booleancomponentSpecified,

ActivityRecord[] outActivity,ActivityStackSupervisor.ActivityContainer container,

TaskRecord inTask)(AS.java 其中有两个返回值代表启动Activity失败:

START_INTENT_NOT_RESOLVED: 从Intent中无法找到相应的Component或者ActivityInfo

START_NOT_CURRENT_USER_ACTIVITY:该Activity对当前用户不可见)——》

16、步骤15中进入boolean checkAppSwitchAllowedLocked(intsourcePid, intsourceUid,

intcallingPid, intcallingUid,String name)(AMS.java 当mAppSwitchesAllowedTime时间小于当前时长,或者具有STOP_APP_SWITCHES的权限,则允许app发生切换操作.

其中mAppSwitchesAllowedTime, 在AMS.stopAppSwitches()的过程中会设置为:mAppSwitchesAllowedTime = SystemClock.uptimeMillis() + APP_SWITCH_DELAY_TIME. 禁止app切换的timeout时长为5s(APP_SWITCH_DELAY_TIME = 5s).

当发送5秒超时或者执行AMS.resumeAppSwitches()过程会将mAppSwitchesAllowedTime设置0, 都会开启允许app执行切换的操作.另外,禁止App切换的操作,对于同一个app是不受影响的,有兴趣可以进一步查看checkComponentPermission过程.)

17、步骤15中进入void doPendingActivityLaunchesLocked(booleandoResume)(AS.java mPendingActivityLaunches记录着所有将要启动的Activity, 是由于在startActivityLocked的过程时App切换功能被禁止, 也就是不运行切换Activity, 那么此时便会把相应的Activity加入到mPendingActivityLaunches队列. 该队列的成员在执行完doPendingActivityLaunchesLocked便会清空.

启动mPendingActivityLaunches中所有的Activity, 由于doResume = false, 那么这些activtity并不会进入resume状态,而是设置delayedResume = true, 会延迟resume.)

18、步骤15中进入

int startActivityUnchecked(finalActivityRecord r,ActivityRecord sourceRecord,

IVoiceInteractionSession voiceSession,IVoiceInteractor voiceInteractor,

intstartFlags, booleandoResume,ActivityOptions options,TaskRecord inTask)(AStarter.java  找到或创建新的Activit所属于的Task对象,之后调用AStack.startActivityLocked)——》

19、void startActivityLocked(ActivityRecord r, booleannewTask, booleankeepCurTransition,

ActivityOptions options)(ActivityStack.java)——》

20、void validateAppTokensLocked()(ActivityStack.java)——》

21、void validateAppTokens(int stackId,List tasks)(WindowManagerService.java)——》

22、

2.10 AS.startActivityLocked(书签)

你可能感兴趣的:(Activity启动流程)