之前的博客中提到过,在启动一个新Activity时,如果界面还存在其它的Activity,那么必须先中断其它的Activity。
因此,除了第一个启动的Home界面对应的Activity外,其它的Activity均需要进行此操作,详细过程在启动Activity的过程:一的第六部分进行了描述。
现在我们可以分析一下,这段之前略去的代码流程。
一、startPausingLocked函数
根据启动Activity的过程:一, 我们知道在启动Activity的过程中,当已经将Activity对应的Task移动到前台,同时将待启动的Activity放置到栈顶后,将会调用ActivityStack中的resumeTopActivityInnerLocked函数,在该函数中:
private boolean resumeTopActivityInnerLocked(......) {
...........
//mResumedActivity保存者当前在前台显示的Activity
if (mResumedActivity != null) {
.............
//调用startPausingLocked函数,中断正在显示的Activity
pausing |= startPausingLocked(userLeaving, false, true, dontWaitForPause);
}
if (pausing) {
..........
return true;
} ..........
............
}
此处,跟进一下startPausingLocked函数:
//Start pausing the currently resumed activity.
final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping, boolean resuming,
boolean dontWait) {
.................
//mResumedActivity保存着当前正在显示的Activity
ActivityRecord prev = mResumedActivity;
.................
mResumedActivity = null;
//mPausingActivity保存准备中断的Activity
mPausingActivity = prev;
................
//更新状态
prev.state = ActivityState.PAUSING;
................
if (prev.app != null && prev.app.thread != null) {
................
try {
............
//通知该Activity所在的进程,调用schedulePauseActivity函数
prev.app.thread.schedulePauseActivity(prev.appToken, prev.finishing,
userLeaving, prev.configChangeFlags, dontWait);
} catch (Exception e) {
...............
}
} else {
..............
}
// If we are not going to sleep, we want to ensure the device is
// awake until the next activity is started.
if (!uiSleeping && !mService.isSleepingOrShuttingDownLocked()) {
//在启动Activity的第二部分中提过
//该WakeLock防止在Activity切换过程中,系统发生休眠
//当Activity切换成功后,将会释放该WakeLock
mStackSupervisor.acquireLaunchWakelock();
}
if (mPausingActivity != null) {
// Have the window manager pause its key dispatching until the new
// activity has started. If we're pausing the activity just because
// the screen is being turned off and the UI is sleeping, don't interrupt
// key dispatch; the same activity will pick it up again on wakeup.
if (!uiSleeping) {
//暂停输入事件的派发
prev.pauseKeyDispatchingLocked();
} ........
.........
if (dontWait) {
// If the caller said they don't want to wait for the pause, then complete
// the pause now.
completePauseLocked(false);
return false