Android 7.0 ActivityManagerService(4) 启动Activity的过程:三

之前的博客中提到过,在启动一个新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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值