Android | 基于Android9.0的startActivity流程分析(1):原Activity的onPause流程

本文章基于Android9.0,测试demo为在一个已经启动的MainActivity中点击按钮通过调用startActivity启动SecondActivity,通过在源码中添加log定位分析整个流程。

启动操作:

startActivity(new Intent(this, SecondActivity.class));

其实在前台Activity中启动另一个Activity的过程主要分为以下几步:

  1. 原Activity的onPause
  2. 新Activity的onCreate
  3. 新Activity的onStart
  4. 新Activity的onResume
  5. 原Activity的onStop
  6. 原Activity的onSaveInstanceState

本文章也主要围绕这个几个流程来分析源码,如有不对之处,欢迎交流指正。

1. 原Activity的onPause流程

Demo是在继承在Activity的子类MainActivity中调用的startActivity,那么就看看Activity.startActivity函数。

[/frameworks/base/core/java/android/app/Activity.java]

    @Override
    public void startActivity(Intent intent) {
        this.startActivity(intent, null);
    }
     @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        if (options != null) {
            startActivityForResult(intent, -1, options);
        } else { //执行else中的操作
            // Note we want to go through this call for compatibility with
            // applications that may have overridden the method.
            startActivityForResult(intent, -1); 
        }
    }

调用startActivityForResult。

public void startActivityForResult(@RequiresPermission Intent intent, int requestCode) {
        startActivityForResult(intent, requestCode, null);
}
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode,
            @Nullable Bundle options) {
        if (mParent == null) {
            options = transferSpringboardActivityOptions(options);
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options); //调用Instrumentation.execStartActivity
            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                .....
            }

            cancelInputsAndStartExitTransition(options);
            // TODO Consider clearing/flushing other event sources and events for child windows.
        } else {
            .....
        }
    }

接着来看Instrumentation.execStartActivity

[/framework/base/core/java/android/app/Instrumentation.java]

public ActivityResult execStartActivity(
            Context who, IBinder contextThread, IBinder token, Activity target,
            Intent intent, int requestCode, Bundle options) {
        IApplicationThread whoThread = (IApplicationThread) contextThread;
		
        ......
		
        try {
            intent.migrateExtraStreamToClipData();
            intent.prepareToLeaveProcess(who);
            int result = ActivityManager.getService()
                .startActivity(whoThread, who.getBasePackageName(), intent,
                        intent.resolveTypeIfNeeded(who.getContentResolver()),
                        token, target != null ? target.mEmbeddedID : null,
                        requestCode, 0, null, options);  //调用AMS.startActivity
            checkStartActivityResult(result, intent); //检查startActivity的结果
        } catch (RemoteException e) {
            throw new RuntimeException("Failure from system", e);
        }
        return null;
}

该函数主要完成的操作:

  • AMS.startActivity:启动新的Activity
  • checkStartActivityResult:检查新的Activity的启动结果,检查结束后,startActivity的调用也就结束了。

其中ActivityManager.getService()就是获取到AMS的句柄,然后调用AMS.startActivity。

[/frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java]

    @Override
    public final int startActivity(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions) {
        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
                resultWho, requestCode, startFlags, profilerInfo, bOptions,
                UserHandle.getCallingUserId());
    }
    @Override
    public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
        return startActivityAsUser(caller, callingPackage, intent, resolvedType, resultTo,
                resultWho, requestCode, startFlags, profilerInfo, bOptions, userId,
                true /*validateIncomingUser*/);
    }
public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
            Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
            int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId,
            boolean validateIncomingUser) {
        enforceNotIsolatedCaller("startActivity");
        userId = mActivityStartController.checkTargetUser(userId, validateIncomingUser,
                Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");
        // TODO: Switch to user app stacks here.
        return mActivityStartController.obtainStarter(intent, "startActivityAsUser")
                .setCaller(caller)
                .setCallingPackage(callingPackage)
                .setResolvedType(resolvedType)
                .setResultTo(resultTo)
                .setResultWho(resultWho)
                .setRequestCode(requestCode)
                .setStartFlags(startFlags)
                .setProfilerInfo(profilerInfo)
                .setActivityOptions(bOptions)
                .setMayWait(userId)
                .execute();
    }

其中mActivityStartController的声明:

private final ActivityStartController mActivityStartController;

mActivityStarterController.obtainStarter函数返回一个ActivityStarter对象:

[/frameworks/base/services/core/java/com/android/server/am/ActivityStartController.java]

    /**
     * @return A starter to configure and execute starting an activity. It is valid until after
     *         {@link ActivityStarter#execute} is invoked. At that point, the starter should be
     *         considered invalid and no longer modified or used.
     */
    ActivityStarter obtainStarter(Intent intent, String reason) {
        return mFactory.obtain().setIntent(intent).setReason(reason);
    }

根据注释可以看到,在ActivityStarter.execute函数执行后才有效。结合AMS.startActivityAsUser函数,先设置各项参数,然后再调用ActivityStarter.execute执行后续的操作。接下里看看ActivityStarter.execute:

[/frameworks/base/services/core/java/com/android/server/am/ActivityStarter.java]

/**
     * Starts an activity based on the request parameters provided earlier.
     * @return The starter result.
     */
    int execute() {
        try {
            // TODO(b/64750076): Look into passing request directly to these methods to allow
            // for transactional diffs and preprocessing.
            if (mRequest.mayWait) {
                return startActivityMayWait(mRequest.caller, mRequest.callingUid,
                        mRequest.callingPackage, mRequest.intent, mRequest.resolvedType,
                        mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
                        mRequest.resultWho, mRequest.requestCode, mRequest.startFlags,
                        mRequest.profilerInfo, mRequest.waitResult, mRequest.globalConfig,
                        mRequest.activityOptions, mRequest.ignoreTargetSecurity, mRequest.userId,
                        mRequest.inTask, mRequest.reason,
                        mRequest.allowPendingRemoteAnimationRegistryLookup);
            } else {
                return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent,
                        mRequest.resolvedType, mRequest.activityInfo, mRequest.resolveInfo,
                        mRequest.voiceSession, mRequest.voiceInteractor, mRequest.resultTo,
                        mRequest.resultWho, mRequest.requestCode, mRequest.callingPid,
                        mRequest.callingUid, mRequest.callingPackage, mRequest.realCallingPid,
                        mRequest.realCallingUid, mRequest.startFlags, mRequest.activityOptions,
                        mRequest.ignoreTargetSecurity, mRequest.componentSpecified,
                        mRequest.outActivity, mRequest.inTask, mRequest.reason,
                        mRequest.allowPendingRemoteAnimationRegistryLookup);
            }
        } finally {
            onExecutionComplete();
        }
    }

因为之前的设置,mayWait = true,所以执行的startActivityMayWait,所以接着来看看这个函数。该函数比较长,仅阐述其中的关键流程:

private int startActivityMayWait(IApplicationThread caller, int callingUid,
            String callingPackage, Intent intent, String resolvedType,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int startFlags,
            ProfilerInfo profilerInfo, WaitResult outResult,
            Configuration globalConfig, SafeActivityOptions options, boolean ignoreTargetSecurity,
            int userId, TaskRecord inTask, String reason,
            boolean allowPendingRemoteAnimationRegistryLookup) {
        ......
        mSupervisor.getActivityMetricsLogger().notifyActivityLaunching(); //提示Activity正在启动
        
		......

        synchronized (mService) {
            ......
            
            final ActivityRecord[] outRecord = new ActivityRecord[1];
            //调用startActivity
            int res = startActivity(caller, intent, ephemeralIntent, resolvedType, aInfo, rInfo,
                    voiceSession, voiceInteractor, resultTo, resultWho, requestCode, callingPid,
                    callingUid, callingPackage, realCallingPid, realCallingUid, startFlags, options,
                    ignoreTargetSecurity, componentSpecified, outRecord, inTask, reason,
                    allowPendingRemoteAnimationRegistryLookup);

            ...

            mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(res, outRecord[0]); //提示Activity启动完成
            return res;
        }
    }
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            SafeActivityOptions options, boolean ignoreTargetSecurity, boolean componentSpecified,
            ActivityRecord[] outActivity, TaskRecord inTask, String reason,
            boolean allowPendingRemoteAnimationRegistryLookup) {

        .....
		//调用startActivity,没有reason参数
        mLastStartActivityResult = startActivity(caller, intent, ephemeralIntent, resolvedType,
                aInfo, rInfo, voiceSession, voiceInteractor, resultTo, resultWho, requestCode,
                callingPid, callingUid, callingPackage, realCallingPid, realCallingUid, startFlags,
                options, ignoreTargetSecurity, componentSpecified, mLastStartActivityRecord,
                inTask, allowPendingRemoteAnimationRegistryLookup);

        if (outActivity != null) {
            // mLastStartActivityRecord[0] is set in the call to startActivity above.
            outActivity[0] = mLastStartActivityRecord[0];
        }

        return getExternalResult(mLastStartActivityResult);
    }
private int startActivity(IApplicationThread caller, Intent intent, Intent ephemeralIntent,
            String resolvedType, ActivityInfo aInfo, ResolveInfo rInfo,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            IBinder resultTo, String resultWho, int requestCode, int callingPid, int callingUid,
            String callingPackage, int realCallingPid, int realCallingUid, int startFlags,
            SafeActivityOptions options,
            boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
            TaskRecord inTask, boolean allowPendingRemoteAnimationRegistryLookup) {
        int err = ActivityManager.START_SUCCESS;
        
		......//错误判断以及权限检测

        return startActivity(r, sourceRecord, voiceSession, voiceInteractor, startFlags,
                true /* doResume */, checkedOptions, inTask, outActivity); //调用startActivity
    }
private int startActivity(final ActivityRecord r, ActivityRecord sourceRecord,
                IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
                int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
                ActivityRecord[] outActivity) {
        int result = START_CANCELED;
        try {
            mService.mWindowManager.deferSurfaceLayout(); //暂停布局
            result = startActivityUnchecked(r, sourceRecord, voiceSession, voiceInteractor,
                    startFlags, doResume, options, inTask, outActivity); //关键函数
        } finally {
            // If we are not able to proceed, disassociate the activity from the task. Leaving an
            // activity in an incomplete state can lead to issues, such as performing operations
            // without a window container.
            final ActivityStack stack = mStartActivity.getStack();
            if (!ActivityManager.isStartResultSuccessful(result) && stack != null) { //如果启动失败的处理
                stack.finishActivityLocked(mStartActivity, RESULT_CANCELED,
                        null /* intentResultData */, "startActivity", true /* oomAdj */);
            }
            mService.mWindowManager.continueSurfaceLayout(); //继续布局
        }

        postStartActivityProcessing(r, result, mTargetStack);

        return result;
    }

该函数里主要的执行的操作:

  • WMS.deferSurfaceLayout:暂停布局
  • ActivityStarter.startActivityUnchecked:关键函数,启动Activity
  • WMS.continueSurfaceLayout:继续布局
  • ActivityStarter.postStartActivityPorcessing

看关键函数startActivityUnchecked,该函数较长,仅说明关键流程:

// Note: This method should only be called from {@link startActivity}.
    private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,
            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
            int startFlags, boolean doResume, ActivityOptions options, TaskRecord inTask,
            ActivityRecord[] outActivity) {

        setInitialState(r, options, inTask, doResume, startFlags, sourceRecord, voiceSession,
                voiceInteractor); //初始化启动Activity的各种配置,会先重置再重新配置,其中包括ActivityRecord、Intent、TaskRecord和LaunchFlags(启动的FLAG)等等

        computeLaunchingTaskFlags(); //计算出启动的FLAG,并将计算的值赋值给mLaunchFlags

        computeSourceStack(); 

        mIntent.setFlags(mLaunchFlags);//将mLaunchFlags设置给Intent,达到设定Activity的启动方式的目的

        ActivityRecord reusedActivity = getReusableIntentActivity(); //获取可复用Activity

        ......

        if (reusedActivity != null) { //可复用Activity不为空
            ......
        }

        ......
		
        // Should this be considered a new task?
        int result = START_SUCCESS;
        if (mStartActivity.resultTo == null && mInTask == null && !mAddingToTask
                && (mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) != 0) {
            newTask = true;
            result = setTaskFromReuseOrCreateNewTask(taskToAffiliate, topStack);
        } else if (mSourceRecord != null) { //根据Demo的情况,会执行这个if语句
            result = setTaskFromSourceRecord();
        } else if (mInTask != null) {
            result = setTaskFromInTask();
        } else {
            // This not being started from an existing activity, and not part of a new task...
            // just put it in the top task, though these days this case should never happen.
            setTaskToCurrentTopOrCreateNewTask();
        }
        if (result != START_SUCCESS) {
            return result;
        }

        ......
		
        if (mDoResume) {
            final ActivityRecord topTaskActivity =
                    mStartActivity.getTask().topRunningActivityLocked();
            if (!mTargetStack.isFocusable()
                    || (topTaskActivity != null && topTaskActivity.mTaskOverlay
                    && mStartActivity != topTaskActivity)) {
                ......
            } else {
                // If the target stack was not previously focusable (previous top running activity
                // on that stack was not visible) then any prior calls to move the stack to the
                // will not update the focused stack.  If starting the new activity now allows the
                // task stack to be focusable, then ensure that we now update the focused stack
                // accordingly.
                if (mTargetStack.isFocusable() && !mSupervisor.isFocusedStack(mTargetStack)) {
                    mTargetStack.moveToFront("startActivityUnchecked");
                }
				
                mSupervisor.resumeFocusedStackTopActivityLocked(mTargetStack, mStartActivity,
                        mOptions);//关键函数
            }
        } else if (mStartActivity != null) {
            mSupervisor.mRecentTasks.add(mStartActivity.getTask());
        }
		
        ......

        return START_SUCCESS;
    }

private void computeLaunchingTaskFlags() {
      ...
      //如下code主要解决的问题就是Activity要加入的栈不存在时如何计算出启动的FLAG
      if (mInTask == null) {//mInTask为null,说明Activity要加入的栈不存在
            if (mSourceRecord == null) {//源Activity,即启动待启动Activity的Activity。如ActivityA启动了ActivityB,那么ActivityA就是源Activity
                if ((mLaunchFlags & FLAG_ACTIVITY_NEW_TASK) == 0 && mInTask == null) {//条件满足则创建一个新栈
                    Slog.w(TAG, "startActivity called from non-Activity context; forcing " +
                            "Intent.FLAG_ACTIVITY_NEW_TASK for: " + mIntent);
                    mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
                }
            } else if (mSourceRecord.launchMode == LAUNCH_SINGLE_INSTANCE) {//如果源Activity所在的栈只允许有一个实例,也需要创建一个新栈
                mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
            } else if (mLaunchSingleInstance || mLaunchSingleTask) {//如果LaunchMode设置了singleTask或singleInstance,则也要创建一个新栈
                mLaunchFlags |= FLAG_ACTIVITY_NEW_TASK;
            }
        }
}

该函数主要完成的操作:

  • ActivityStarter.setInitialState
  • ActivityStarter.computeLaunchingTaskFlags
  • ActivityStarter.computeSourceTask
  • ActivityStarter.getResuableIntentActivity
  • ActivityStarter.setTaskFromSourceRecord
  • ActivityStackSupervisor.resumeFocusedStackTopActivityLocked:关键函数

来看看关键函数resumeFocusedStackTopActivityLocked:

// /frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
boolean resumeFocusedStackTopActivityLocked(
            ActivityStack targetStack, ActivityRecord target, ActivityOptions targetOptions) {

        ......

        if (targetStack != null && isFocusedStack(targetStack)) { //执行该条件
            return targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);
        }

        ......

        return false;
    }

主要完成的操作是调用ActivityStack.resumeTopActivityUncheckedLocked函数:

// /frameworks/base/services/core/java/com/android/server/am/ActivityStack.java
@GuardedBy("mService")
    boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {
        ...
		
        try {
            ......
			
            result = resumeTopActivityInnerLocked(prev, options);

            ...
			
            final ActivityRecord next = topRunningActivityLocked(true /* focusableOnly */);
            if (next == null || !next.canTurnScreenOn()) {
                checkReadyForSleep();
            }
        } finally {
            mStackSupervisor.inResumeTopActivity = false;
        }

        return result;
    }

主要完成的操作:

  • ActivityStack.resumeTopActivityInnerLocked:关键函数
  • ActivityStack.topRunningActivityLocked

看看关键函数resumeTopActivityInnerLocked,该函数较长,主要的操作是ActivityStack.startPausingLocked:

@GuardedBy("mService")
    private boolean resumeTopActivityInnerLocked(ActivityRecord prev, ActivityOptions options) {
    ......

    boolean pausing = mStackSupervisor.pauseBackStacks(userLeaving, next, false);
    if (mResumedActivity != null) {
         pausing |= startPausingLocked(userLeaving, false, next, false);
    }

    ......
}

startPausingLocked函数就是将原Activity进行pause操作:

final boolean startPausingLocked(boolean userLeaving, boolean uiSleeping,
            ActivityRecord resuming, boolean pauseImmediately) {
        ...

        if (prev.app != null && prev.app.thread != null) {
            try {
                ...
				
                mService.getLifecycleManager().scheduleTransaction(prev.app.thread, prev.appToken,
                        PauseActivityItem.obtain(prev.finishing, userLeaving,
                                prev.configChangeFlags, pauseImmediately));  //关键操作
            } catch (Exception e) {
                .....
            }
        } else {
            ...
        }
		
		...
    }

此处的mService是指的AMS,mService.getLifecycleManager()获取了ClientLifecycleManager对象。ClientLifecycleManaer.sheduleTransaction函数管控了Activity的生命周期函数的执行。

// /frameworks/base/services/core/java/com/android/server/am/ClientLifecycleManager.java     
    /**
     * Schedule a single lifecycle request or callback to client activity.
     * @param client Target client.
     * @param activityToken Target activity token.
     * @param stateRequest A request to move target activity to a desired lifecycle state.
     * @throws RemoteException
     *
     * @see ClientTransactionItem
     */
    void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken,
            @NonNull ActivityLifecycleItem stateRequest) throws RemoteException {
        final ClientTransaction clientTransaction = transactionWithState(client, activityToken,
                stateRequest);
        scheduleTransaction(clientTransaction);
    }

其中的stateRequest参数就是上文的PauseActivityItem对象。该函数中完成的主要操作:

  • transactionWithState
    /**
     * @return A new instance of {@link ClientTransaction} with a single lifecycle state request.
     *
     * @see ClientTransaction
     * @see ClientTransactionItem
     */
    private static ClientTransaction transactionWithState(@NonNull IApplicationThread client,
            @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest) {
        final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken);
        clientTransaction.setLifecycleStateRequest(stateRequest);
        return clientTransaction;
    }

将PauseActivityItem对象设置到clientTransaction对象中。并返回该clientTransaction对象。

  • scheduleTransaction
    void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
        final IApplicationThread client = transaction.getClient();
        transaction.schedule(); //调用ClientTransaction的schedule函数
        if (!(client instanceof Binder)) {
            // If client is not an instance of Binder - it's a remote call and at this point it is
            // safe to recycle the object. All objects used for local calls will be recycled after
            // the transaction is executed on client in ActivityThread.
            transaction.recycle();
        }
    }

接下来看看ClientTransaction.schedule函数:

// /frameworks/base/core/java/android/app/servertransaction/ClientTransaction.java
    
    private IApplicationThread mClient;

    public void schedule() throws RemoteException {
        mClient.scheduleTransaction(this);
    }

mClient实质就是应用进程与系统进程(system_server)通信的服务端,即ActivityThread.ApplicaitonThread对象。所以接下来调用到了ActivityThread.ApplicationThread.sheduleTransaction函数:

// /frameworks/base/core/java/android/app/ActivityThread.java
        @Override
        public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
            ActivityThread.this.scheduleTransaction(transaction);
        }

ActivityThread中没有定义函数scheduleTransaction,因为ActivityThread继承于ClientTransactionHandler,所以查看ActivityThread的父类中该函数。

// /frameworks/base/core/java/android/app/ClientTransactionHandler.java
    /** Prepare and schedule transaction for execution. */
    void scheduleTransaction(ClientTransaction transaction) {
        transaction.preExecute(this);
        sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
    }
    
    abstract void sendMessage(int what, Object obj);

sendMessage为抽象函数,ActivityThread实现了该函数。所以此处又调用到了ActivityThread.sendMessage函数中:

 ///frameworks/base/core/java/android/app/ActivityThread.java
    void sendMessage(int what, Object obj) {
        sendMessage(what, obj, 0, 0, false);
    }

    private void sendMessage(int what, Object obj, int arg1, int arg2, boolean async) {
        if (DEBUG_MESSAGES) Slog.v(
            TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
            + ": " + arg1 + " / " + obj);
        Message msg = Message.obtain();
        msg.what = what;
        msg.obj = obj;
        msg.arg1 = arg1;
        msg.arg2 = arg2;
        if (async) {
            msg.setAsynchronous(true);
        }
        mH.sendMessage(msg);
    }

其中的mH为ActivityThread内部类H的一个对象,用于处理Message。此前通过sendMessage发送的消息为H.EXECUTE_TRANSACTION,所以来看看mH是如何处理这个消息的。

// /frameworks/base/core/java/android/app/ActivityThread.java
private final TransactionExecutor mTransactionExecutor = new TransactionExecutor(this);

//ActivityThread.H的handleMessage函数
case EXECUTE_TRANSACTION:
    final ClientTransaction transaction = (ClientTransaction) msg.obj;
    mTransactionExecutor.execute(transaction);
    if (isSystem()) {
        // Client transactions inside system process are recycled on the client side
        // instead of ClientLifecycleManager to avoid being cleared before this
        // message is handled.
        transaction.recycle();
    }
    // TODO(lifecycler): Recycle locally scheduled transactions.
    break;                

接下来看看TransactionExecutor.execute函数做了些什么:

    /**
     * Resolve transaction.
     * First all callbacks will be executed in the order they appear in the list. If a callback
     * requires a certain pre- or post-execution state, the client will be transitioned accordingly.
     * Then the client will cycle to the final lifecycle state if provided. Otherwise, it will
     * either remain in the initial state, or last state needed by a callback.
     */
    public void execute(ClientTransaction transaction) {
        final IBinder token = transaction.getActivityToken();
        log("Start resolving transaction for client: " + mTransactionHandler + ", token: " + token);

        executeCallbacks(transaction);

        executeLifecycleState(transaction);
        mPendingActions.clear();
        log("End resolving transaction");
    }

execute函数中主要完成的操作是:

  • executeCallbacks:因为此前没有传入callback,所以callback为空,未做任何实际的操作。
  • executeLifecycleState
// /frameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java
    /** Transition to the final state if requested by the transaction. */
    private void executeLifecycleState(ClientTransaction transaction) {
        final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest(); //根据前文描述,lifecycleItem就是PauseActivityItem对象
        if (lifecycleItem == null) {
            // No lifecycle request, return early.
            return;
        }
        log("Resolving lifecycle state: " + lifecycleItem);

        final IBinder token = transaction.getActivityToken();
        final ActivityClientRecord r = mTransactionHandler.getActivityClient(token);

        if (r == null) {
            // Ignore requests for non-existent client records for now.
            return;
        }

        // Cycle to the state right before the final requested state.
        cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */);

        // Execute the final transition with proper parameters.
        lifecycleItem.execute(mTransactionHandler, token, mPendingActions);
        lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);
    }

即该函数中主要完成的操作:(1) cycleToPath ; (2)PauseActivityitem.execute ; (3) PauseActivityItem.postExecute。接着来分析一下这三个过程。

  • cycleToPath
// /frameworks/base/core/java/android/app/servertransaction/TransactionExecutor.java
    private void cycleToPath(ActivityClientRecord r, int finish,
            boolean excludeLastState) {
        final int start = r.getLifecycleState();
        log("Cycle from: " + start + " to: " + finish + " excludeLastState:" + excludeLastState);
        final IntArray path = mHelper.getLifecyclePath(start, finish, excludeLastState);
        performLifecycleSequence(r, path);
    }

根据demo的case情况,path.size = 0,所以performLifecycleSequence函数并未执行任何操作。

  • PauseActivityItem.execute
// /frameworks/base/core/java/android/app/servertransaction/PauseActivityItem.java
    @Override
    public void execute(ClientTransactionHandler client, IBinder token,
            PendingTransactionActions pendingActions) {
        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
        client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions,
                "PAUSE_ACTIVITY_ITEM");
        Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
    }

关键的操作就是调用了ClientTransactionHandler.handlePauseActivity,而在ClientTransactionHandler类中,handlePauseActivity是个抽象函数,其实现在ActivityThread类中。也即是此处调用了ActivityThread.handlePauseActivity。

///frameworks/base/core/java/android/app/ActivityThread.java
	@Override
    public void handlePauseActivity(IBinder token, boolean finished, boolean userLeaving,
            int configChanges, PendingTransactionActions pendingActions, String reason) {
        ActivityClientRecord r = mActivities.get(token);
        if (r != null) {
            if (userLeaving) {
                performUserLeavingActivity(r);
            }

            r.activity.mConfigChangeFlags |= configChanges;
            performPauseActivity(r, finished, reason, pendingActions);

            // Make sure any pending writes are now committed.
            if (r.isPreHoneycomb()) {
                QueuedWork.waitToFinish();
            }
            mSomeActivitiesChanged = true;
        }
    }

继续调用ActivityThread.performPauseActivity

private Bundle performPauseActivity(ActivityClientRecord r, boolean finished, String reason,
            PendingTransactionActions pendingActions) {
        ...

        // Pre-Honeycomb apps always save their state before pausing
        final boolean shouldSaveState = !r.activity.mFinished && r.isPreHoneycomb(); //值为false
        if (shouldSaveState) {
            callActivityOnSaveInstanceState(r);
        }

        performPauseActivityIfNeeded(r, reason);

        ...

        return shouldSaveState ? r.state : null;
    }

再看看performPauseActivityIfNeeded:

    private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {
        if (r.paused) {
            // You are already paused silly...
            return;
        }

        try {
            r.activity.mCalled = false;
            mInstrumentation.callActivityOnPause(r.activity);  //关键操作
            if (!r.activity.mCalled) {
                throw new SuperNotCalledException("Activity " + safeToComponentShortString(r.intent)
                        + " did not call through to super.onPause()");
            }
        } catch (SuperNotCalledException e) {
            throw e;
        } catch (Exception e) {
            if (!mInstrumentation.onException(r.activity, e)) {
                throw new RuntimeException("Unable to pause activity "
                        + safeToComponentShortString(r.intent) + ": " + e.toString(), e);
            }
        }
        r.setState(ON_PAUSE);
    }

又调用了Instrumentation.callActivityOnPause函数:

// /frameworks/base/core/java/android/app/Instrumentation.java
	/**
     * Perform calling of an activity's {@link Activity#onPause} method.  The
     * default implementation simply calls through to that method.
     *
     * @param activity The activity being paused.
     */
    public void callActivityOnPause(Activity activity) {
        activity.performPause();
    }	

又调用了Activity的performPause函数:

// /frameworks/base/core/java/android/app/Activity.java
    final void performPause() {
        mDoReportFullyDrawn = false;
        mFragments.dispatchPause(); //处理Fragment的pause操作
        mCalled = false;
        onPause();  //执行onPause函数
        writeEventLog(LOG_AM_ON_PAUSE_CALLED, "performPause");
        mResumed = false;
        if (!mCalled && getApplicationInfo().targetSdkVersion
                >= android.os.Build.VERSION_CODES.GINGERBREAD) {
            throw new SuperNotCalledException(
                    "Activity " + mComponent.toShortString() +
                    " did not call through to super.onPause()");
        }
    }

至此,原Activity的onPause函数得到了执行

在此总结一下该流程,因为调用层次较深,所以使用了一个思维导图来描述调用层次。

Android | 基于Android9.0的startActivity流程分析(1):原Activity的onPause流程_第1张图片

  • PauseActivityItem.postExecute

该函数涉及到了新的Activity的启动流程,后续再接着分析。先看一下该函数的code。

    @Override
    public void postExecute(ClientTransactionHandler client, IBinder token,
            PendingTransactionActions pendingActions) {
        if (mDontReport) {
            return;
        }
        try {
            // TODO(lifecycler): Use interface callback instead of AMS.
            ActivityManager.getService().activityPaused(token); //跨进程调用AMS.activityPaused函数
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

 

你可能感兴趣的:(#,Activity,Activity)