Activity启动流程(基于Android10附关键代码及流程图)
我们在平常开发的过程中,想启动⼀个Activity怎么办?startActivity(Intent intent)就可以了,这是⼀个简单得不能在简单的问题了。但如果问你启动⼀个Activity具体经过了哪些步骤,即时在同⼀App内调⽤startActivity是否⽤到了进程间通信呢?是不是有点蒙,我⼀个App内启动⼀个Activity还会⽤到进程间通信,要搞基(什么)?那么,我们就具体来看⼀看吧,也许这种知识在99%的开发中我们都不可能会⽤到,但多了解⼀些对我们也没什么坏处吧。
先上⼀张启动调⽤图吧,有点乱,将就看吧。谁让俺们对画这种图不专业呢。
毫⽆疑问,不管你是继承的什么Activity,启动都是从startActivity开始
Activity类
public void startActivity(Intent intent){
this.startActivity(intent, null);
}
//内部的重载⽅法
public void startActivity(Intent intent,@Nullable Bundle options){
if(options != null){
startActivityForResult(intent,-1, options);
}else{
// Note we want to go through this call for compatibility with
// applications that may have overridden the method.
startActivityForResult(intent,-1);
}
}
//调⽤到startActivityForResult
public void startActivityForResult(@RequiresPermission Intent intent,int requestCode){
startActivityForResult(intent, requestCode, null);
}
//⼜调⽤了重载的startActivityForResult
public void startActivityForResult(@RequiresPermission Intent intent,int requestCode,
@Nullable Bundle options){
//关键代码
Instrumentation.ActivityResult ar =
this, ApplicationThread(), mToken,this,
intent, requestCode, options);
}
调⽤StartActivity这时候进⼊到了Instrumentation类
Instrumentation类
public ActivityResult execStartActivity(
Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent,int requestCode, Bundle options){
//关键代码
int result = Service()
.startActivity(whoThread, BasePackageName(), intent,
token, target != null ? target.mEmbeddedID : null,
requestCode,0, null, options);
}
看下ActivityTaskManager类 调⽤getService的内容如下
public static IActivityTaskManager getService(){
();
}
private static final Singleton<IActivityTaskManager> IActivityTaskManagerSingleton =
new Singleton<IActivityTaskManager>(){
@Override
protected IActivityTaskManager create(){
final IBinder b = Service(Context.ACTIVITY_TASK_SERVICE);
return IActivityTaskManager.Stub.asInterface(b);
}
};
注意看IActivityTaskManagerSingleton⾥⾯实例的时候得到的是IBinder,是否似曾相识?AIDL⽤过没?⼀模⼀样的⽤法。注意,这时候开始我们进⼊系统的进程,已经不在本进程了哈。正式登陆⽕星。
public class ActivityTaskManagerService extends IActivityTaskManager.Stub
这⾥贴出这个类的关系主要是表明ActivityTaskManagerService就是IActivityTaskManager.Stub
这时候我们有得进到ActivityTaskManagerService这个类了,startActivity这个⽅法
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,
}
//调⽤startActivityAsUser
public int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho,int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions,int userId){
return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
resultWho, requestCode, startFlags, profilerInfo, bOptions, userId,
true/*validateIncomingUser*/);
}
//调⽤重载⽅法startActivityAsUser
int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho,int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions,int userId,
boolean validateIncomingUser){
//关键代码
return getActivityStartController().obtainStarter(intent,"startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setResolvedType(resolvedType)
.
setResultTo(resultTo)
.setResultWho(resultWho)
.setRequestCode(requestCode)
.setStartFlags(startFlags)
.setProfilerInfo(profilerInfo)
.setActivityOptions(bOptions)
.setMayWait(userId)
.execute();
}
obtainStarter返回⼀个ActivityStarter类实例,注意下setMayWait这个,对我们后续流程分⽀有影响。
ActivityStarter类
/
/setMayWait将mayWait设置为了true
ActivityStarter setMayWait(int userId){
mRequest.mayWait =true;
mRequest.userId = userId;
return this;
}
}
int execute(){
try{
//setMayWait决定了我们将⾛if分⽀参数多得⼀匹
if(mRequest.mayWait){
return startActivityMayWait(mRequest.caller, mRequest.callingUid,
mRequest.callingPackage, alCallingPid, alCallingUid,                        mRequest.intent, solvedType,
mRequest.voiceSession, mRequest.voiceInteractor, sultTo,
mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig,
mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId,
mRequest.inTask, ason,
mRequest.allowPendingRemoteAnimationRegistryLookup,
}else{
//
}finally{
onExecutionComplete();
}
}
private int startActivityMayWait(IApplicationThread caller,int callingUid,
String callingPackage,int requestRealCallingPid,int requestRealCallingUid,
Intent intent, String resolvedType, IVoiceInteractionSession voiceSession,
IVoiceInteractor voiceInteractor, IBinder resultTo, String resultWho,int requestCode, int startFlags, ProfilerInfo profilerInfo, WaitResult outResult,
Configuration globalConfig, SafeActivityOptions options,boolean ignoreTargetSecurity, int userId, TaskRecord inTask, String reason,
boolean allowPendingRemoteAnimationRegistryLookup,
PendingIntentRecord originatingPendingIntent,boolean allowBackgroundActivityStart){ //关键代码
int res =startActivity(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo,
voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid,
callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options,
ignoreTargetSecurity, componentSpecified, outRecord, inTask, reason,
allowPendingRemoteAnimationRegistryLookup, originatingPendingIntent,
allowBackgroundActivityStart);
}
//进⼊到这个⽅法
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho,int requestCode,int callingPid,int callingUid,
String callingPackage,int realCallingPid,int realCallingUid,int startFlags,
SafeActivityOptions options,boolean ignoreTargetSecurity,boolean componentSpecified,            ActivityRecord[] outActivity, TaskRecord inTask, String reason,
boolean allowPendingRemoteAnimationRegistryLookup,
PendingIntentRecord originatingPendingIntent,boolean allowBackgroundActivityStart){ //关键代码
mLastStartActivityResult =startActivity(caller, intent, ephemeralIntent, resolvedType,
aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
inTask, allowPendingRemoteAnimationRegistryLookup, originatingPendingIntent,
allowBackgroundActivityStart);
}
//进⼊这个重载⽅法
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
IBinder resultTo, String resultWho,int requestCode,int callingPid,int callingUid,
String callingPackage,int realCallingPid,int realCallingUid,int startFlags,
SafeActivityOptions options,
boolean ignoreTargetSecurity,boolean componentSpecified, ActivityRecord[] outActivity,            TaskRecord inTask,boolean allowPendingRemoteAnimationRegistryLookup,
PendingIntentRecord originatingPendingIntent,boolean allowBackgroundActivityStart){
PendingIntentRecord originatingPendingIntent,boolean allowBackgroundActivityStart){ //关键代码
final int res =startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags, true/* doResume */, checkedOptions, inTask, outActivity, restrictedBgActivity);
}
//⼜进⼊了这个重载,好吧,我承认我有点晕车
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity,boolean restrictedBgActivity){
//关键代码
result =startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
startFlags, doResume, options, inTask, outActivity, restrictedBgActivity);
}
//调⽤startActivityUnchecked
private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, int startFlags,boolean doResume, ActivityOptions options, TaskRecord inTask,
ActivityRecord[] outActivity,boolean restrictedBgActivity){
//关键代码
if(dontStart){
//⾮常关键前⾯我们都会到startActivity系列,但这⾥需要从这⾥⾛,不然会迷路的
if(mDoResume){
}
}
}
进⼊RootActivityContainer类
boolean resumeFocusedStacksTopActivities(){
return resumeFocusedStacksTopActivities(null, null, null);
}
//调⽤重载⽅法
boolean resumeFocusedStacksTopActivities(
ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions){ //关键代码
if(targetStack != null &&(targetStack.isTopStackOnDisplay()
||getTopDisplayFocusedStack()== targetStack)){
result = sumeTopActivityUncheckedLocked(target, targetOptions);
}
}
targetStack是ActivityStack类的实例,所以进⼊ActivityStack类了
制作android软件流程boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options){ //关键代码
try{
result =resumeTopActivityInnerLocked(prev, options);
}finally{
mInResumeTopActivity =false;
}
}
private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options){ //关键代码
mStackSupervisor.startSpecificActivityLocked(next,true,true);
}
进⼊ActivityStackSupervisor类
void startSpecificActivityLocked(ActivityRecord r,boolean andResume,boolean checkConfig){
//关键代码
try{
realStartActivityLocked(r, wpc, andResume, checkConfig);
return;
}catch(RemoteException e){
Slog.w(TAG,"Exception when starting activity "
+ Component().flattenToShortString(), e);
}
}
boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,
boolean andResume,boolean checkConfig)throws RemoteException{
//关键代码
// Schedule transaction.
}
mService是ActivityTaskManagerService的实例 getLifecycleManager
ClientLifecycleManager getLifecycleManager(){
return mLifecycleManager;
}
返回的是ClientLifecycleManager
进⼊ClientLifecycleManager
void scheduleTransaction(ClientTransaction transaction)throws RemoteException {
final IApplicationThread client = Client();
//关键代码
transaction.schedule();
if(!(client instanceof Binder)){
// If client is not an instance of Binder - it's a remote call and at this point it is
// safe to recycle the object. All objects used for local calls will be recycled after
/
/ the transaction is executed on client in ActivityThread.
}
}
transation是ClientTransation的实例,进⼊到了ClientTransaction类
public void schedule()throws RemoteException {
mClient.scheduleTransaction(this);
}
看下mClient的定义
private IApplicationThread mClient;
是不是瞬间千万匹飘过,点不进去了啊。莫慌,他明显是⼀个接⼝类,⽽且前⾯我们不是看到了AIDL么,感觉是不是要让我们回地球的感觉了呢?
其实现类在ActivityThread类的⼀个内部类ApplicationThread,看下这个类吧
private class ApplicationThread extends IApplicationThread.Stub
嗯嗯,⼜给续上了。⽽且成功让我们从⽕星回到了地球(我们的APP进程)
public void scheduleTransaction(ClientTransaction transaction)throws RemoteException {
ActivityThread.this.scheduleTransaction(transaction);
}
嗯,这⾥⾯就⼀句代码,调⽤了 ActivityThread.this.scheduleTransaction(transaction);
其调⽤了⽗类ClientTransactionHandler的

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。