建议先看另一篇blog:
https://blog.csdn.net/learnframework/article/details/123032419
系统如何让桌面执行对应的onStart方法呢?
具体的堆栈显示如下:
makeActiveIfNeeded:5788, ActivityRecord (com.android.server.wm)
makeVisibleIfNeeded:5697, ActivityRecord (com.android.server.wm)
setActivityVisibilityState:210, EnsureActivitiesVisibleHelper (com.android.server.wm)
process:143, EnsureActivitiesVisibleHelper (com.android.server.wm)
updateActivityVisibilities:1117, TaskFragment (com.android.server.wm)
lambda$ensureActivitiesVisible$18:4857, Task (com.android.server.wm)
accept:-1, Task$$ExternalSyntheticLambda23 (com.android.server.wm)
forAllLeafTasks:3174, Task (com.android.server.wm)
forAllLeafTasks:3162, Task (com.android.server.wm)
ensureActivitiesVisible:4856, Task (com.android.server.wm)
lambda$ensureActivitiesVisible$45:6293, DisplayContent (com.android.server.wm)
accept:-1, DisplayContent$$ExternalSyntheticLambda38 (com.android.server.wm)
forAllRootTasks:3186, Task (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2014, WindowContainer (com.android.server.wm)
forAllRootTasks:2007, WindowContainer (com.android.server.wm)
ensureActivitiesVisible:6292, DisplayContent (com.android.server.wm)
ensureActivitiesVisible:1864, RootWindowContainer (com.android.server.wm)
ensureActivitiesVisible:1845, RootWindowContainer (com.android.server.wm)
startRecentsActivity:260, RecentsAnimation (com.android.server.wm)
startRecentsActivity:1729, ActivityTaskManagerService (com.android.server.wm)
onTransact:1171, IActivityTaskManager$Stub (android.app)
onTransact:5183, ActivityTaskManagerService (com.android.server.wm)
execTransactInternal:1280, Binder (android.os)
execTransact:1244, Binder (android.os)
导致onStart调用关键就在于这里的startRecentsActivity会触发ensureActivitiesVisible方法,正常的ensureActivitiesVisible只是来保证当前一些ActivityRecord的属性值或者config变化了,靠调用ensureActivitiesVisible来保证Activity的visibility可以显示正常
这里的
targetActivity.mLaunchTaskBehind = true;
就是最为关键的点,它代表者可以在其他activity的后面可以显示。
疑问为啥只是到了onStart而不走到onResume:
根据上面的堆栈来看这个核心方法makeActiveIfNeeded
boolean makeActiveIfNeeded(ActivityRecord activeActivity) {
//判断Activity是应该被Resumeif (shouldResumeActivity(activeActivity)) {return getRootTask().resumeTopActivityUncheckedLocked(activeActivity /* prev */,null /* options */);} else if (shouldPauseActivity(activeActivity)) {
//判断Activity是应该被PasuesetState(PAUSING, "makeActiveIfNeeded");mAtmService.getLifecycleManager().scheduleTransaction(app.getThread(), token,PauseActivityItem.obtain(finishing, false /* userLeaving */,configChangeFlags, false /* dontReport */));} else if (shouldStartActivity()) {setState(STARTED, "makeActiveIfNeeded");try {mAtmService.getLifecycleManager().scheduleTransaction(app.getThread(), token,StartActivityItem.obtain(takeOptions()));} mTaskSupervisor.mStoppingActivities.remove(this);}return false;}
那么这里其实最主要我们关注是shouldResumeActivity(activeActivity)方法为啥没有返回true,而返回来false,导致进入到了shouldStartActivity方法
看看shouldResumeActivity方法:
@VisibleForTestingboolean shouldResumeActivity(ActivityRecord activeActivity) {//主要调用到了shouldBeResumedreturn shouldBeResumed(activeActivity) && !isState(RESUMED);}private boolean shouldBeResumed(ActivityRecord activeActivity) {//shouldBeResumed方法又有若干条件,任何条件不满足都是不可以的,先看看第一个条件shouldMakeActivereturn shouldMakeActive(activeActivity) && isFocusable()&& getTaskFragment().getVisibility(activeActivity)== TASK_FRAGMENT_VISIBILITY_VISIBLE&& canResumeByCompat();}boolean shouldMakeActive(ActivityRecord activeActivity) {if (!isState(STARTED, RESUMED, PAUSED, STOPPED, STOPPING)// TODO (b/185876784) Check could we remove the check condition// mTranslucentActivityWaiting != null here|| getRootTask().mTranslucentActivityWaiting != null) {return false;}if (this == activeActivity) {return false;}if (!mTaskSupervisor.readyToResume()) {// Making active is currently deferred (e.g. because an activity launch is in progress).return false;}
//核心就是这个地方,前面设置了mLaunchTaskBehind为true,所以这里就返回false了if (this.mLaunchTaskBehind) {// This activity is being launched from behind, which means that it's not intended to be// presented to user right now, even if it's set to be visible.return false;}return getTaskFragment().topRunningActivity() == this;}