四大组件的启动
Activity的启动
1.startActivity
- 通常使用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 {
startActivityForResult(intent, -1);
}
}
pulic void startActivityForResult(Intent intent, int requestCode) {
startActivityForResult(intent, requestCode, null);
}
2.startActivityForResult
public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
if (mParent == null) {
options = transferSpringboardActivityOptions(options);
Instrumenttation.execStartActivity(this, mMainTread.getApplicationThread(), mToken, this, intent, requestCode, options);
else {
if (options != null) {
mParents.startActivityFromChild(this, intent, requestCode, options);
} else {
mParents.startActivityFromChidl(this, intent, requestCode);
}
}
3.Instrumentation.execStartActivity
- 这个工具类用来监视应用进程和系统交互的,调用该方法,其内部实际调用了
ActivityTaskManager.getService().startActivity
函数,这个类是在SystemServer
里面的,看到这个进程,就知道这是个系统级别的东西,很重要
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity tartget, Intent intent, Int requestCode, Bundle otions) {
IApplicationTread whoThread = (IApplicationThread) contextThread;
...
try {
int result = ActivityTaskManager.getService().startActivity(.......);
} catch (RemoteException e) {
.......
return null;
}
4.ATMS.startActivity
- ATMS就是ActivityTaskManagerService
- 类似的ConnectivityManager, ConnectivityService, 都是在Manager中定义一堆接口,然后在service中是具体的实现
pulic int startActivityAsUser(......) {
return startActivityAsUser(........, UserHandle.getCallingUserId());
}
- 这个函数的作用就是检查包名和userId是否一致等安全权限问题
pulic int startActivityAsUser(........., int userId) {
return startActivityAsUser(..........,userId, true);
}
pulic int startActivityAsUser(........., int userId, boolean validateIncomingUser) {
asserPackageMatchesCallingUid(callingPackage);
enforceNotIsolatedCaller("startActivityAsUser");
return getActivityStartController().obtainStarter(intent, "startActivityAsUser")
.setCaller(caller)
.setCallingPackage(callingPackage)
.setCallingFeatureId(callingFeatureId)
..........
.setUser(userId)
.execute();
}
5.ActivityStarter.execute
- execute -> executRequest
- 因为内部代码比较复杂,这里就不放,总之干了一件事,把上面含有一堆信息Build出的对象,封装成了一个
ActivityRecord
对象,然后调用它的startActivityUnchecked
方法
pulic int startActivityUnchecked(.........) {
int result = START_CANCELED;
..........
result = startActivityInner(.......);
.........
return result;
}
int startActivityInner(...........) {
.......
mRootWindowContainer.resumeFocusedStacksTopActivities(......)
return START_SUCCESS;
}
6.中间的一系列省略掉,最后调用了Activity.performCreate
final void performCreate(Bundle icicle, PersistableBundle persistentState) {
dispatchActivityPreCreated(icicle);
mCanEnterPictureInPicture = true;
final int windowingMode = getResources().getConfiguration().windowConfiguration
.getWindowingMode();
mIsInMultiWindowMode = inMultiWindowMode(windowingMode);
mIsInPictureInPictureMode = windowingMode == WINDOWING_MODE_PINNED;
restoreHasCurrentPermissionRequest(icicle);
if (persistentState != null) {
onCreate(icicle, persistentState);
} else {
onCreate(icicle);
}
EventLogTags.writeWmOnCreateCalled(mIdent, getComponentName().getClassName(),
"performCreate");
mActivityTransitionState.readState(icicle);
mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(
com.android.internal.R.styleable.Window_windowNoDisplay, false);
mFragments.dispatchActivityCreated();
mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());
dispatchActivityPostCreated(icicle);
}