当前位置:首页 > Android 7.0 ActivityManagerService(4) 启动Activity的过程:三
Android 7.0 ActivityManagerService(4)
启动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; } else {
// Schedule a pause timeout in case the app doesn't respond. // We don't give it much time because this directly impacts the // responsiveness seen by the user.
Message msg = mHandler.obtainMessage(PAUSE_TIMEOUT_MSG); msg.obj = prev;
prev.pauseTime = SystemClock.uptimeMillis(); //延迟时间500ms,当这个消息被处理时,也会调用completePauseLocked函数
mHandler.sendMessageDelayed(msg, PAUSE_TIMEOUT); } } else {
............. } }
容易看出,这段代码最主要的操作是与应用进程的ApplicationThread进行Binder通信,调用其schedulePauseActivity函数。
与之前的流程一样,ApplicationThread仅作为通信接口,它将发送消息触发进程的ActivityThread调用handlePauseActivity进行实际的操作。
二、handlePauseActivity函数
private void handlePauseActivity(IBinder token, boolean finished,
boolean userLeaving, int configChanges, boolean dontReport, int seq) { ActivityClientRecord r = mActivities.get(token); ..............
if (r != null) { ..............
r.activity.mConfigChangeFlags |= configChanges; //执行pause的实际操作
performPauseActivity(token, finished, r.isPreHoneycomb(), \ ..............
// Tell the activity manager we have paused. if (!dontReport) { try {
//通知AMS
ActivityManagerNative.getDefault().activityPaused(token); } catch (RemoteException ex) {
throw ex.rethrowFromSystemServer(); } }
mSomeActivitiesChanged = true; } }
这段代码比较简单,就是调用performPauseActivity完成中断Activity的实际工作,然后再通过Binder通信通知AMS中断完成。 我们稍微看一下performPauseActivity函数:
final Bundle performPauseActivity(IBinder token, boolean finished, boolean saveState, String reason) {
ActivityClientRecord r = mActivities.get(token);
return r != null ? performPauseActivity(r, finished, saveState, reason) : null; }
final Bundle performPauseActivity(ActivityClientRecord r, boolean finished, boolean saveState, String reason) { ................
// Next have the activity save its current state and managed dialogs... if (!r.activity.mFinished && saveState) {
//完成调用Activity的onSaveInstanceState接口等操作 callCallActivityOnSaveInstanceState(r); }
//调用Activity的onPause接口
performPauseActivityIfNeeded(r, reason);
// Notify any outstanding on paused listeners ArrayList
//ActivityThread提供了对外接口registerOnActivityPausedListener //可以注册观察者监听某个Activity进入paused状态 listeners = mOnPauseListeners.remove(r.activity); }
int size = (listeners != null ? listeners.size() : 0); for (int i = 0; i < size; i++) { //回调
listeners.get(i).onPaused(r.activity); }
return !r.activity.mFinished && saveState ? r.state : null; }
performPauseActivity整体的逻辑很清晰,就是调用Activity生命周期中对应的接口,同时通知观察者Activity paused。
现在我们将视线移回到AMS,看看定义于其中的activityPaused函数。
public final void activityPaused(IBinder token) { final long origId = Binder.clearCallingIdentity(); synchronized(this) {
ActivityStack stack = ActivityRecord.getStackLocked(token); if (stack != null) {
stack.activityPausedLocked(token, false); }
共分享92篇相关文档