【Android】Activity-R 完整启动流程

1,从startActivity说起:

@Override

public void startActivity(Intent intent) {

    this.startActivity(intent, null);

}

startActivityForResult(intent, -1, options);


Activity里面有一个重要的成员变量 mInstrumentation,该变量用于调度Activity各项生命活动,非常重要。当start一个Activity时,调用该成员execStartActivity方法。

Instrumentation.ActivityResult ar =

    mInstrumentation.execStartActivity(//下一步

        this, mMainThread.getApplicationThread(), mToken, this,

        intent, requestCode, options);

ar即是启动Activity的结果,后面对启动结果进行某种解析,稍后再谈。

if (ar != null) {

//发送启动结果到mainThread,看名字应该是主线程

mMainThread.sendActivityResult(

mToken, mEmbeddedID, requestCode, ar.getResultCode(),

ar.getResultData());

}


2,进入Instrumentation中:

关键点在一次binder过程,即调用ATMS的startActivity()函数,

int result = ActivityTaskManager.getService().startActivity(whoThread,

        who.getBasePackageName(), who.getAttributionTag(), intent,

        intent.resolveTypeIfNeeded(who.getContentResolver()), token,

        target != null ? target.mEmbeddedID : null, requestCode, 0null, options);

//检查结果

checkStartActivityResult(result, intent);


3,进入ATMS中,查看startActivity函数

//来到系统进程

@Override

public final int startActivity(IApplicationThread caller, String callingPackage,

        String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,

        String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,

        Bundle bOptions) {

    //远程调用到这。 记录客户端第一次调用,接下来,callback自己的startActivityAsUser

    return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,

            resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions,

            UserHandle.getCallingUserId());

}

@Override

public int startActivityAsUser(IApplicationThread caller, String callingPackage,

        String callingFeatureId, Intent intent, String resolvedType, IBinder resultTo,

        String resultWho, int requestCode, int startFlags, ProfilerInfo profilerInfo,

        Bundle bOptions, int userId) {

    //继续callback自己

    return startActivityAsUser(caller, callingPackage, callingFeatureId, intent, resolvedType,

            resultTo, resultWho, requestCode, startFlags, profilerInfo, bOptions, userId,

            true /*validateIncomingUser*/);

}

private int startActivityAsUser(IApplicationThread caller, String callingPackage,

        @Nullable String callingFeatureId, Intent intent, String resolvedType,

        IBinder resultTo, String resultWho, int requestCode, int startFlags,

        ProfilerInfo profilerInfo, Bundle bOptions, int userId, boolean validateIncomingUser) {

    assertPackageMatchesCallingUid(callingPackage);

    enforceNotIsolatedCaller("startActivityAsUser");

    userId = getActivityStartController().checkTargetUser(userId, validateIncomingUser,

            Binder.getCallingPid(), Binder.getCallingUid(), "startActivityAsUser");

    // TODO: Switch to user app stacks here.

    //此处切换到用户进程stack

    //获得一个启动器,此处采用的是享元模式

    return getActivityStartController().obtainStarter(intent, "startActivityAsUser")

            .setCaller(caller)

            .setCallingPackage(callingPackage)

            .setCallingFeatureId(callingFeatureId)

            .setResolvedType(resolvedType)

            .setResultTo(resultTo)

            .setResultWho(resultWho)

            .setRequestCode(requestCode)

            .setStartFlags(startFlags)

            .setProfilerInfo(profilerInfo)

            .setActivityOptions(bOptions)

            .setUserId(userId)

            //执行

            .execute();

}

获取ActivityStartController,通过享元模式从pool中拿到一个ActivityStarter,在通过构造者模式设置参数信息,比如设置回调、启动参数options等。

下一步,调用ActivityStarter的execute方法。


4,进入ActivityStarter:

在该类中,有一个重要成员 mRequest,显而易见,这是保存启动Activity所有参数的集合类,其set方法列举了所有参数,

void set(Request request) {

    caller = request.caller;

    intent = request.intent;

    intentGrants = request.intentGrants;

    ephemeralIntent = request.ephemeralIntent;

    resolvedType = request.resolvedType;

    activityInfo = request.activityInfo;

    resolveInfo = request.resolveInfo;

    voiceSession = request.voiceSession;

    voiceInteractor = request.voiceInteractor;

    resultTo = request.resultTo;

    resultWho = request.resultWho;

    requestCode = request.requestCode;

    callingPid = request.callingPid;

    callingUid = request.callingUid;

    callingPackage = request.callingPackage;

    callingFeatureId = request.callingFeatureId;

    realCallingPid = request.realCallingPid;

    realCallingUid = request.realCallingUid;

    startFlags = request.startFlags;

    activityOptions = request.activityOptions;

    ignoreTargetSecurity = request.ignoreTargetSecurity;

    componentSpecified = request.componentSpecified;

    outActivity = request.outActivity;

    inTask = request.inTask;

    reason = request.reason;

    profilerInfo = request.profilerInfo;

    globalConfig = request.globalConfig;

    userId = request.userId;

    waitResult = request.waitResult;

    avoidMoveToFront = request.avoidMoveToFront;

    allowPendingRemoteAnimationRegistryLookup

            = request.allowPendingRemoteAnimationRegistryLookup;

    filterCallingUid = request.filterCallingUid;

    originatingPendingIntent = request.originatingPendingIntent;

    allowBackgroundActivityStart = request.allowBackgroundActivityStart;

}


request在execute中被使用,

//执行请求,获得结果

res = executeRequest(mRequest);

executeRequest函数注释:

执行活动启动请求,开启活动启动之旅。首先是执行几个初步检查。
通常的 Activity 启动流程会经过 {@link startActivityUnchecked} 到 {@link startActivityInner}。
于是乎,对request参数做检查后,一切没什么问题了,调用如下

//开始启动

mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession,

        request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, inTask,

        restrictedBgActivity, intentGrants);

/**

 * 在完成大部分初步检查并确认调用者拥有执行此操作所需的权限后开始活动。如果启动不成功,这里还确保删除启动活动。

 */

private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord,

            IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

            int startFlags, boolean doResume, ActivityOptions options, Task inTask,

            boolean restrictedBgActivity, NeededUriGrants intentGrants) {

    //此处,启动活动,因为参数已经验证过,这儿不验证参数,

    //进一步调用

    result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor,

            startFlags, doResume, options, inTask, restrictedBgActivity, intentGrants);

...}

/**

 * 启动活动并确定活动是否应添加到现有任务的顶部或向现有活动传递新意图。还将活动任务操作到请求的或有效的堆栈显示上。注意:此方法只能从 {@link startActivityUnchecked} 调用。

 */

@VisibleForTesting

int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord,

        IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,

        int startFlags, boolean doResume, ActivityOptions options, Task inTask,

        boolean restrictedBgActivity, NeededUriGrants intentGrants) {

    //此函数的功能,如果没有Stack,就要新创建一个Stack,也就是ActivityStack,保存到mTargetTask中,通过setNewTask将待启动的ActivityRecord和新创建的Stack绑定,并且将其置于Stack Top,主要通过以下函数实现该绑定

    mTargetStack.startActivityLocked(mStartActivity, topFocused,newTask, mKeepCurTransition, mOptions);

    //如果一个从未启动过的Activity,那么最终调用如下函数,传入参数,TargetStack和StartActivity,目标栈和待启动Activity

    mRootWindowContainer.resumeFocusedStacksTopActivities(mTargetStack, mStartActivity, mOptions);

    ...

}

5,进入RootWindowContainer:

调用ActivityStack,进入ActivityStack中调度

//resumeFocusedStacksTopActivities函数,

if (targetStack != null && (targetStack.isTopStackInDisplayArea()

        || getTopDisplayFocusedStack() == targetStack)) {

    // 第一次进入 需要pause ResumedActivity

    result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);

}

6,进入ActivityStack:

@GuardedBy("mService")

boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {

//此处拿到的是待启动Activity,和prev相等,毕竟新创建的Stack中只存在一个MainActivity

ActivityRecord next = topRunningActivity(true /* focusableOnly */);

//传入next,通过与resumedActivity进行判断,是否相等,来pause掉已经resume的Activity

boolean pausing = taskDisplayArea.pauseBackStacks(userLeaving, next);

//拿到top运行的Activity,暂停它,由于返回值pausing为true,那么resumeTopActivityUncheckedLocked直接返回,等待上一个Stack中的Activity暂停后通知该系统resumeNext;

        // END

        if (pausing) {

            if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,

                    "resumeTopActivityLocked: Skip resume: need to start pausing");

            // At this point we want to put the upcoming activity's process

            // at the top of the LRU list, since we know we will be needing it

            // very soon and it would be a waste to let it get killed if it

            // happens to be sitting towards the end.

            if (next.attachedToProcess()) {

                next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,

                        true /* activityChange */false /* updateOomAdj */,

                        false /* addPendingTopUid */);

            else if (!next.isProcessRunning()) {

                // Since the start-process is asynchronous, if we already know the process of next

                // activity isn't running, we can start the process earlier to save the time to wait

                // for the current activity to be paused.

                final boolean isTop = this == taskDisplayArea.getFocusedStack();

                mAtmService.startProcessAsync(next, false /* knownToBeDead */, isTop,

                        isTop ? "pre-top-activity" "pre-activity");

            }

            if (lastResumed != null) {

                lastResumed.setWillCloseOrEnterPip(true);

            }

            return true;

        }

...}

7,进入TaskDisplayArea:

/**

 * 暂停所有堆栈或仅后堆栈中的所有活动。这是在恢复新活动之前完成的,并确保先前活动的活动在不再可见的堆栈中或处于固定窗口模式中暂停。

 * 这不会暂停可见堆栈中的活动,因此如果在同一个堆栈任务中启动了一个活动,那么我们应该明确地暂停该堆栈的顶部活动。

 * @param userLeaving 传递给 pauseActivity() 以指示是否调用 onUserLeaving()。

 * @param resume 正在恢复的活动。

 * @return {@code true} 如果由于此调用而暂停了任何活动。

 */

boolean pauseBackStacks(boolean userLeaving, ActivityRecord resuming) {...

    //对要pause的Activity逐一pause,传入的resuming是待启动Activity,主要在pauseBackStacks中调用,用于启动Activity

    someActivityPaused |= stack.startPausingLocked(userLeaving, false /* uiSleeping*/,resuming);

}

8,进入ActivityStack:

注意此次的Stack是上一个Activity的Stack,即马上pause的Stack,startPausingLocked函数中,设置prev状态为当前正在pause的Activity,然后,执行pause事务

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

    ........

    ActivityRecord prev = mResumedActivity;

    ........

    mPausingActivity = prev;

    mLastPausedActivity = prev;

    mLastNoHistoryActivity = prev.isNoHistory() ? prev : null;

    prev.setState(PAUSING, "startPausingLocked");

    prev.getTask().touchActiveTime();

    clearLaunchTime(prev);

    ........

    //getLifecycleManager拿到ClientLifecycleManager,调用scheduleTransaction函数执行pause事务,

    mAtmService.getLifecycleManager().scheduleTransaction(prev.app.getThread(),prev.appToken, PauseActivityItem.obtain(prev.finishing, userLeaving,

                       prev.configChangeFlags, pauseImmediately));

    .......

    // 等待pause后启动下一个Activity,

    completePauseLocked(false, resuming);

    .......

}

9,进入ClientLifecycleManager:

根据client和stateRequest生成transaction,进一步调用schedule方法,处理事务,transaction是一个ClientTransaction类,

void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken,

        @NonNull ActivityLifecycleItem stateRequest) throws RemoteException {

    //一个binder过程,这次来到的是即将pause的用户进程

    final ClientTransaction clientTransaction = transactionWithState(client, activityToken,

            stateRequest);

    scheduleTransaction(clientTransaction);

}

void scheduleTransaction(ClientTransaction transaction) throws RemoteException {

    //获得事务 客户端

    final IApplicationThread client = transaction.getClient();

    //执行

    transaction.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();

    }

}

10,进入ClientTransaction:

以下调用拿到即将pause的用户进程Binder,即一次IPC,在用户进程执行事务,client是一个IApplicationThread

public void schedule() throws RemoteException {

    mClient.scheduleTransaction(this);

}

11,进入ActivityThread:

来到即将pause用户进程

@Override

public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {

    //activityThread 处,执行事务pause

    ActivityThread.this.scheduleTransaction(transaction);

}

ActivityThread下有个Handler,专门用来处理消息。如下,发送消息,类型为EXECUTE_TRANSACTION

/** Prepare and schedule transaction for execution. */

void scheduleTransaction(ClientTransaction transaction) {

transaction.preExecute(this);

//在此处直接去看ActivityThread的handler

sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);

}

//handleMessage片段如下,msg.obj存放的就是transaction,那么在成员变量mTransactionExecutor解析事务,

case EXECUTE_TRANSACTION:

    //执行生命周期的事务

    final ClientTransaction transaction = (ClientTransaction) msg.obj;

    //Executor 执行事务

    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;

12,进入TransactionExecutor:

/**

 * 解决交易。 首先,所有回调将按照它们出现在列表中的顺序执行。 如果回调需要特定的执行前或执行后状态,客户端将相应地转换。

 * 然后客户端将循环到最终生命周期状态(如果提供)。 否则,它要么保持初始状态,要么保持回调所需的最后状态

 */

public void execute(ClientTransaction transaction) {

    if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "Start resolving transaction");

    final IBinder token = transaction.getActivityToken();

    if (token != null) {

        // 对destroy的Activity单独处理

        final Map<IBinder, ClientTransactionItem> activitiesToBeDestroyed =

                mTransactionHandler.getActivitiesToBeDestroyed();

        final ClientTransactionItem destroyItem = activitiesToBeDestroyed.get(token);

        if (destroyItem != null) {

            if (transaction.getLifecycleStateRequest() == destroyItem) {

                // It is going to execute the transaction that will destroy activity with the

                // token, so the corresponding to-be-destroyed record can be removed.

                activitiesToBeDestroyed.remove(token);

            }

            if (mTransactionHandler.getActivityClient(token) == null) {

                // The activity has not been created but has been requested to destroy, so all

                // transactions for the token are just like being cancelled.

                Slog.w(TAG, tId(transaction) + "Skip pre-destroyed transaction:\n"

                        + transactionToString(transaction, mTransactionHandler));

                return;

            }

        }

    }

    if (DEBUG_RESOLVER) Slog.d(TAG, transactionToString(transaction, mTransactionHandler));

    //执行回调

    executeCallbacks(transaction);

    //走生命周期

    executeLifecycleState(transaction);

    mPendingActions.clear();

    if (DEBUG_RESOLVER) Slog.d(TAG, tId(transaction) + "End resolving transaction");

}

如果事务中设置了callback,先executeCallbacks,由于当前Activity需要pause,即没有设置回调,直接看生命周期处理,executeLifecycleState,

/** Transition to the final state if requested by the transaction. 如果事务请求,则转换到最终状态。*/

private void executeLifecycleState(ClientTransaction transaction) {

    //返回5种item 对应 pause resume destroy start stop

    // 这里返回的是pauseitem

    final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest();

    if (lifecycleItem == null) {

        // No lifecycle request, return early.

        return;

    }

    final IBinder token = transaction.getActivityToken();

    final ActivityClientRecord r = mTransactionHandler.getActivityClient(token);

    if (DEBUG_RESOLVER) {

        Slog.d(TAG, tId(transaction) + "Resolving lifecycle state: "

                + lifecycleItem + " for activity: "

                + getShortActivityName(token, mTransactionHandler));

    }

    if (r == null) {

        // Ignore requests for non-existent client records for now.

        return;

    }

    // Cycle to the state right before the final requested state.

    // pause后应该stop,

    cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);

    // Execute the final transition with proper parameters.

    // 执行最终确定的周期状态

    // 此处,根据前面设定的下一个周期状态,执行,

    // pauseActivityItem

    lifecycleItem.execute(mTransactionHandler, token, mPendingActions);

    // pause后 处理结果

    lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);

}

execute函数走PauseActivityItem,

13,进入PauseActivityItem:

@Override

public void execute(ClientTransactionHandler client, IBinder token,

        PendingTransactionActions pendingActions) {

    Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityPause");

    // 处理 pause

    client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions,

            "PAUSE_ACTIVITY_ITEM");

    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);

}

handlePauseActivity中,调用performPauseActivity,

private Bundle performPauseActivity(ActivityClientRecord r, boolean finished, String reason,

        PendingTransactionActions pendingActions) {

    if (r.paused) {

      // 已经paused了,简单报个错

        if (r.activity.mFinished) {

            // If we are finishing, we won't call onResume() in certain cases.

            // So here we likewise don't want to call onPause() if the activity

            // isn't resumed.

            return null;

        }

        RuntimeException e = new RuntimeException(

                "Performing pause of activity that is not resumed: "

                + r.intent.getComponent().toShortString());

        Slog.e(TAG, e.getMessage(), e);

    }

    if (finished) {

        r.activity.mFinished = true;

    }

    // Pre-Honeycomb apps always save their state before pausing

    final boolean shouldSaveState = !r.activity.mFinished && r.isPreHoneycomb();

    if (shouldSaveState) {

        //主动保存状态,回调onSaveInstanceState,

        callActivityOnSaveInstanceState(r);

    }

    //执行pause

    performPauseActivityIfNeeded(r, reason);

    // Notify any outstanding on paused listeners

    ArrayList<OnActivityPausedListener> listeners;

    synchronized (mOnPauseListeners) {

        //执行pause listener

        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);

    }

    final Bundle oldState = pendingActions != null ? pendingActions.getOldState() : null;

    if (oldState != null) {

        // We need to keep around the original state, in case we need to be created again.

        // But we only do this for pre-Honeycomb apps, which always save their state when

        // pausing, so we can not have them save their state when restarting from a paused

        // state. For HC and later, we want to (and can) let the state be saved as the

        // normal part of stopping the activity.

        if (r.isPreHoneycomb()) {

            r.state = oldState;

        }

    }

    return shouldSaveState ? r.state : null;

}

private void performPauseActivityIfNeeded(ActivityClientRecord r, String reason) {

    if (r.paused) {

        // You are already paused silly...

        return;

    }

    // Always reporting top resumed position loss when pausing an activity. If necessary, it

    // will be restored in performResumeActivity().

    reportTopResumedActivityChanged(r, false /* onTop */"pausing");

    try {

        r.activity.mCalled = false;

        //回调 onPause

        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);

        }

    }

    // 设置 pause ,完毕

    r.setState(ON_PAUSE);

}

可以看到,此处走Activity的onPause周期,最后设置r的状态为pause,r就是ActivityRecord,事实意义的窗口,

execute完后,回到TransactionExecutor中,执行postExecute函数,处理事件结果,


14,回到TransactionExecutor:

// pause后 处理结果

lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);

15,回到PauseActivityItem:

通知ATMS,已经pause。

@Override

public void postExecute(ClientTransactionHandler client, IBinder token,

        PendingTransactionActions pendingActions) {

    // 进入 pause中 反馈结果

    if (mDontReport) {

        return;

    }

    try {

        // TODO(lifecycler): Use interface callback instead of AMS.

        // 通知atms,已经pause

        ActivityTaskManager.getService().activityPaused(token);

    catch (RemoteException ex) {

        throw ex.rethrowFromSystemServer();

    }

}

16,远程 ActivityTaskManagerService:

回到系统进程,r即是paused的ActivityRecord,处理paused后的一些事务吧,进入ActivityRecord中,

@Override

public final void activityPaused(IBinder token) {

final long origId = Binder.clearCallingIdentity();

synchronized (mGlobalLock) {

Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "activityPaused");

final ActivityRecord r = ActivityRecord.forTokenLocked(token);

if (r != null) {

// 进入

       r.activityPaused(false);

}

       Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);

}

       Binder.restoreCallingIdentity(origId);

}

17,进入ActivityRecord:

r.activityPaused(false)中,stack.completePauseLocked(true /* resumeNext */, null /* resumingActivity */);
进而可看到,要resumeNext了,调用stack,进行调度,
 

18,进入ActivityStack:

@VisibleForTesting

void completePauseLocked(boolean resumeNext, ActivityRecord resuming) {

  ...

 ActivityRecord prev = mPausingActivity;

//该函数完成pause最后的相关事务,比如设置stack中状态,

//对prea的后续处理就是是否加入stop队列等等等等,

//由于传入的resumeNext为true,继续走以下代码,

    if (resumeNext) {

           final ActivityStack topStack = mRootWindowContainer.getTopDisplayFocusedStack();

        if (topStack != null && !topStack.shouldSleepOrShutDownActivities()) {

         //这儿,由于新启动的Activity在新的Stack中,topStack已经指向新启动的Stack,走这一个分支,传入的prev为上一个已经pause的Activity

            mRootWindowContainer.resumeFocusedStacksTopActivities(topStack, prev, null);

    else {

        checkReadyForSleep();

        final ActivityRecord top = topStack != null ? topStack.topRunningActivity() : null;

        if (top == null || (prev != null && top != prev)) {

            mRootWindowContainer.resumeFocusedStacksTopActivities();

        }

     }

    }

}

19,进入RootWindowContainer:

boolean resumeFocusedStacksTopActivities(

     ......

 boolean result = false;

 if (targetStack != null && (targetStack.isTopStackInDisplayArea()

                || getTopDisplayFocusedStack() == targetStack)) {

            result = targetStack.resumeTopActivityUncheckedLocked(target, targetOptions);

  }

}

20,回到ActivityStack:

这个是即将resume的Stack,

@GuardedBy("mService")

boolean resumeTopActivityUncheckedLocked(ActivityRecord prev, ActivityOptions options) {

    if (mInResumeTopActivity) {

        // Don't even start recursing.

        return false;

    }

    boolean result = false;

    try {

        // Protect against recursion.

        mInResumeTopActivity = true;

        // 进入查看

        result = resumeTopActivityInnerLocked(prev, options);

        

        if (next == null || !next.canTurnScreenOn()) {

            checkReadyForSleep();

        }

    finally {

        mInResumeTopActivity = false;

    }

    return result;

}

//resumeTopActivityInnerLocked(prev, options);

//在此时,返回false,

//以下分支在之前需要pause时已经调用过,并且提前返回了,也就是在等待用户端pause事务通知系统进程。此时pausing为false,故该分支后面的没有走,直接调用该函数末尾的startSpecialActivity函数。

boolean pausing = taskDisplayArea.pauseBackStacks(userLeaving, next);

if (pausing) {

    if (DEBUG_SWITCH || DEBUG_STATES) Slog.v(TAG_STATES,

            "resumeTopActivityLocked: Skip resume: need to start pausing");

    if (next.attachedToProcess()) {

        next.app.updateProcessInfo(false /* updateServiceConnectionActivities */,

                true /* activityChange */false /* updateOomAdj */,

                false /* addPendingTopUid */);

    else if (!next.isProcessRunning()) {

        final boolean isTop = this == taskDisplayArea.getFocusedStack();

        mAtmService.startProcessAsync(next, false /* knownToBeDead */, isTop,

                isTop ? "pre-top-activity" "pre-activity");

    }

    if (lastResumed != null) {

        lastResumed.setWillCloseOrEnterPip(true);

    }

    return true;

}

//调用如下函数,开始进入start阶段,启动next所指的Activity

mStackSupervisor.startSpecificActivity(next, truetrue);

21,进入ActivityStackSupervisor:

//注意传入的参数,next是下一个start的ActivityRecord

void startSpecificActivity(ActivityRecord r, boolean andResume, boolean checkConfig) {

    // 开始启动活动了

    final WindowProcessController wpc =

            mService.getProcessController(r.processName, r.info.applicationInfo.uid);

    boolean knownToBeDead = false;

    if (wpc != null && wpc.hasThread()) {

        //不为null,说明进程启动过,调用realStartActivityLocked进行resume即可,这个一般是启动同包下没有设置多进程时进入

        try {

            if (mPerfBoost != null) {

                Slog.i(TAG, "The Process " + r.processName + " Already Exists in BG. So sending its PID: " + wpc.getPid());

                mPerfBoost.perfHint(BoostFramework.VENDOR_HINT_FIRST_LAUNCH_BOOST, r.processName, wpc.getPid(), BoostFramework.Launch.TYPE_START_APP_FROM_BG);

            }

            realStartActivityLocked(r, wpc, andResume, checkConfig);

            return;

        catch (RemoteException e) {

            Slog.w(TAG, "Exception when starting activity "

                    + r.intent.getComponent().flattenToShortString(), e);

        }

        // If a dead object exception was thrown -- fall through to

        // restart the application.

        knownToBeDead = true;

    }

    r.notifyUnknownVisibilityLaunchedForKeyguardTransition();

    final boolean isTop = andResume && r.isTopRunningActivity();

    //如果进程不存在,创建进程:一个新启动的Activity,如果是从桌面启动的,肯定没有process,那么先创建进程,

    mService.startProcessAsync(r, knownToBeDead, isTop, isTop ? "top-activity" "activity");

}

22,进入ATMS:

简单发送一个启动进程的消息 →  ActivityManagerInternal::startProcess,

void startProcessAsync(ActivityRecord activity, boolean knownToBeDead, boolean isTop,

            String hostingType) {

        try {

            if (Trace.isTagEnabled(TRACE_TAG_WINDOW_MANAGER)) {

                Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "dispatchingStartProcess:"

                        + activity.processName);

            }

            // Post message to start process to avoid possible deadlock of calling into AMS with the

            // ATMS lock held.

            final Message m = PooledLambda.obtainMessage(ActivityManagerInternal::startProcess,

                    mAmInternal, activity.processName, activity.info.applicationInfo, knownToBeDead,

                    

                    // isTop, hostingType, activity.intent.getComponent());

                    isTop, hostingType, activity.intent.getComponent(), activity.launchedFromPackage);

            mH.sendMessage(m);

        finally {

            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);

        }

    }

23,进入ActivityManagerInternal,AMS.LocalServer

@Override

public void startProcess(String processName, ApplicationInfo info, boolean knownToBeDead,

        boolean isTop, String hostingType, ComponentName hostingName, String callerPackage) {

        // END

    try {

        if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) {

            Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "startProcess:"

                    + processName);

        }

        synchronized (ActivityManagerService.this) {

            // If the process is known as top app, set a hint so when the process is

            // started, the top priority can be applied immediately to avoid cpu being

            // preempted by other processes before attaching the process of top app.

            // 创建进程

            startProcessLocked(processName, info, knownToBeDead, 0 /* intentFlags */,

                    new HostingRecord(hostingType, hostingName, isTop),

                    ZYGOTE_POLICY_FLAG_LATENCY_SENSITIVE, false /* allowWhileBooting */,

                    // false /* isolated */, true /* keepIfLarge */);

                    false /* isolated */true /* keepIfLarge */, callerPackage);

        }

    finally {

        Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

    }

}

进一步调用AMS的startProcessLocked,

@GuardedBy("this")

final ProcessRecord startProcessLocked(String processName,

        ApplicationInfo info, boolean knownToBeDead, int intentFlags,

        HostingRecord hostingRecord, int zygotePolicyFlags, boolean allowWhileBooting,

        

        // boolean isolated, boolean keepIfLarge) {

        boolean isolated, boolean keepIfLarge, String callerPackage) {

    // 进一步查看

    return mProcessList.startProcessLocked(processName, info, knownToBeDead, intentFlags,

            hostingRecord, zygotePolicyFlags, allowWhileBooting, isolated, 0 /* isolatedUid */,

            keepIfLarge, null /* ABI override */null /* entryPoint */,

            null /* entryPointArgs */null /* crashHandler */, callerPackage);

}

24,进入ProcessList

final boolean success = startProcessLocked(app, hostingRecord, zygotePolicyFlags, abiOverride);

//跟进,最终调用如下,可以看到,返回了进程result,

final Process.ProcessStartResult startResult = startProcess(hostingRecord,

        entryPoint, app,

        uid, gids, runtimeFlags, zygotePolicyFlags, mountExternal, seInfo,

        requiredAbi, instructionSet, invokeWith, startTime);

//处理获得的进程

handleProcessStartedLocked(app, startResult.pid, startResult.usingWrapper,

        startSeq, false);

25,进入Process

// 启动启动进程

startResult = Process.start(entryPoint,

        app.processName, uid, uid, gids, runtimeFlags, mountExternal,

        app.info.targetSdkVersion, seInfo, requiredAbi, instructionSet,

        app.info.dataDir, invokeWith, app.info.packageName, zygotePolicyFlags,

        isTopApp, app.mDisabledCompatChanges, pkgDataInfoMap,

        whitelistedAppDataInfoMap, bindMountAppsData, bindMountAppStorageDirs,

        

        // new String[]{PROC_START_SEQ_IDENT + app.startSeq});

        entryPointArgs);

public static ProcessStartResult start(@NonNull final String processClass,

                                       @Nullable final String niceName,

                                       int uid, int gid, @Nullable int[] gids,

                                       int runtimeFlags,

                                       int mountExternal,

                                       int targetSdkVersion,

                                       @Nullable String seInfo,

                                       @NonNull String abi,

                                       @Nullable String instructionSet,

                                       @Nullable String appDataDir,

                                       @Nullable String invokeWith,

                                       @Nullable String packageName,

                                       int zygotePolicyFlags,

                                       boolean isTopApp,

                                       @Nullable long[] disabledCompatChanges,

                                       @Nullable Map<String, Pair<String, Long>>

                                               pkgDataInfoMap,

                                       @Nullable Map<String, Pair<String, Long>>

                                               whitelistedDataInfoMap,

                                       boolean bindMountAppsData,

                                       boolean bindMountAppStorageDirs,

                                       @Nullable String[] zygoteArgs) {

    //这儿 zygote 进程出现,start!

    return ZYGOTE_PROCESS.start(processClass, niceName, uid, gid, gids,

                runtimeFlags, mountExternal, targetSdkVersion, seInfo,

                abi, instructionSet, appDataDir, invokeWith, packageName,

                zygotePolicyFlags, isTopApp, disabledCompatChanges,

                pkgDataInfoMap, whitelistedDataInfoMap, bindMountAppsData,

                bindMountAppStorageDirs, zygoteArgs);

}

26,进入ZygoteProcess
来到Zygote进程了哦

private Process.ProcessStartResult startViaZygote(...)

//设置启动参数,然后启动,

synchronized(mLock) {

    // The USAP pool can not be used if the application will not use the systems graphics

    // driver.  If that driver is requested use the Zygote application start path.

    // 这里应该是socket通信,把args发给zygote进程,进行fork,

    return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi),

                                      zygotePolicyFlags,

                                      argsForZygote);

}

下一步进入attemptZygoteSendArgsAndGetResult(zygoteState, msgStr);

//显然,这儿涉及socket通信了,是system_service与zygote进程间通信,属于盘古进程间通信

//result就是启动结果,里面写入了新创建进程的pid,usingWrapper信息,写入了result,后面根据result绑定app,

private Process.ProcessStartResult attemptZygoteSendArgsAndGetResult(

        ZygoteState zygoteState, String msgStr) throws ZygoteStartFailedEx {

    try {

        final BufferedWriter zygoteWriter = zygoteState.mZygoteOutputWriter;

        final DataInputStream zygoteInputStream = zygoteState.mZygoteInputStream;

        // 这是socket通信

        zygoteWriter.write(msgStr);//写入创建参数

        zygoteWriter.flush();//发送 清空 意思

        // Always read the entire result from the input stream to avoid leaving

        // bytes in the stream for future process starts to accidentally stumble

        // upon.

        Process.ProcessStartResult result = new Process.ProcessStartResult();

        result.pid = zygoteInputStream.readInt(); // 读取fork的pid,应该是阻塞

        result.usingWrapper = zygoteInputStream.readBoolean();

        if (result.pid < 0) {

            // fork失败

            throw new ZygoteStartFailedEx("fork() failed");

        }

        return result;

    catch (IOException ex) {

        zygoteState.close();

        Log.e(LOG_TAG, "IO Exception while communicating with Zygote - "

                + ex.toString());

        throw new ZygoteStartFailedEx(ex);

    }

}

注意,这是socket通信的客户端,zygote如何创建一个新进程呢?进入zygote中查看,

插叙Zygote创生阶段:系统init阶段,执行了zygote main函数,启动了一个ZygoteService服务,然后进入runSelectPool,即socket监听,

在无限循环中,监听AMS的消息

// Zygote server socket

ZygoteConnection newPeer = acceptCommandPeer(abiList);

peers.add(newPeer);

socketFDs.add(newPeer.getFileDescriptor());

处理收到的消息,返回的command有其艺术感,因为是ActivityThread的main方法!

ZygoteConnection connection = peers.get(pollIndex);

final Runnable command = connection.processOneCommand(this);

// TODO (chriswailes): Is this extra check necessary?

if (mIsForkChild) {

    // We're in the child. We should always have a command to run at

    // this stage if processOneCommand hasn't called "exec".

    if (command == null) {

        throw new IllegalStateException("command == null");

    }

    return command;

}

传说中的孵化过程就在processOneCommand中

//孵化

ZygoteConnection connection = peers.get(pollIndex);

final Runnable command = connection.processOneCommand(this);

if (mIsForkChild) {

    // We're in the child. We should always have a command to run at

    // this stage if processOneCommand hasn't called "exec".

    if (command == null) {

        throw new IllegalStateException("command == null");

    }

    return command;

}

进入processOneCommand中,关键如下,

pid = Zygote.forkAndSpecialize(parsedArgs.mUid, parsedArgs.mGid, parsedArgs.mGids,

        parsedArgs.mRuntimeFlags, rlimits, parsedArgs.mMountExternal, parsedArgs.mSeInfo,

        parsedArgs.mNiceName, fdsToClose, fdsToIgnore, parsedArgs.mStartChildZygote,

        parsedArgs.mInstructionSet, parsedArgs.mAppDataDir, parsedArgs.mIsTopApp,

        parsedArgs.mPkgDataInfoList, parsedArgs.mWhitelistedDataInfoList,

        parsedArgs.mBindMountAppDataDirs, parsedArgs.mBindMountAppStorageDirs);


解释:

因为已经fork了zygote,以下两条分支会在两个进程中得到执行!一个是原本的zygote进程,一个是新生的zygote孵化进程。
pid为0,表示这是一个新的子进程,因为fork之后,可以理解为复制了zygote进程,但pid仍为初始值。如注释,这条路主要用于启动ActivityThread的main方法。
而else语句,fork之后,pid生成的肯定大于0(即子进程的pid),所在进程为zygote进程,继而进入handleParent中,将生成的pid写入socket通道中。

try {

    if (pid == 0) {

        // in child

        zygoteServer.setForkChild();

        zygoteServer.closeServerSocket();

        IoUtils.closeQuietly(serverPipeFd);

        serverPipeFd = null;

        return handleChildProc(parsedArgs, childPipeFd, parsedArgs.mStartChildZygote);

    else {

        // In the parent. A pid < 0 indicates a failure and will be handled in

        // handleParentProc.

        IoUtils.closeQuietly(childPipeFd);

        childPipeFd = null;

        handleParentProc(pid, serverPipeFd);

        return null;

    }

finally {

    IoUtils.closeQuietly(childPipeFd);

    IoUtils.closeQuietly(serverPipeFd);

}

//handleParentProc

mSocketOutStream.writeInt(pid);

mSocketOutStream.writeBoolean(usingWrapper);

在ProcessList中,以下函数将app与pid进行了绑定。

boolean handleProcessStartedLocked(ProcessRecord app, int pid, boolean usingWrapper,

        long expectedStartSeq, boolean procAttached){

          ...

         //绑定进程

         app.setPid(pid);

         app.setUsingWrapper(usingWrapper);

          ...

}

//handleChildProc

//zygote fork之后,出现了两个zygote,其中一个pid为0,这里走child路线,由于肯定不是zygote进程,所以需要关闭socket。

private Runnable handleChildProc(ZygoteArguments parsedArgs,

        FileDescriptor pipeFd, boolean isZygote) {

    /*

     * By the time we get here, the native code has closed the two actual Zygote

     * socket connections, and substituted /dev/null in their place.  The LocalSocket

     * objects still need to be closed properly.

     */

    closeSocket();

    Zygote.setAppProcessName(parsedArgs, TAG);//设置app name

    // End of the postFork event.

    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

    if (parsedArgs.mInvokeWith != null) {

        WrapperInit.execApplication(parsedArgs.mInvokeWith,

                parsedArgs.mNiceName, parsedArgs.mTargetSdkVersion,

                VMRuntime.getCurrentInstructionSet(),

                pipeFd, parsedArgs.mRemainingArgs);

        // Should not get here.

        throw new IllegalStateException("WrapperInit.execApplication unexpectedly returned");

    else {

        if (!isZygote) {

             //不是zygote,走这,

            return ZygoteInit.zygoteInit(parsedArgs.mTargetSdkVersion,

                    parsedArgs.mDisabledCompatChanges,

                    parsedArgs.mRemainingArgs, null /* classLoader */);

        else {

            return ZygoteInit.childZygoteInit(parsedArgs.mTargetSdkVersion,

                    parsedArgs.mRemainingArgs, null /* classLoader */);

        }

    }

}

继续往下走,

public static final Runnable zygoteInit(int targetSdkVersion, long[] disabledCompatChanges,

        String[] argv, ClassLoader classLoader) {

    if (RuntimeInit.DEBUG) {

        Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");

    }

    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");

    RuntimeInit.redirectLogStreams();

    RuntimeInit.commonInit();

    ZygoteInit.nativeZygoteInit();

    return RuntimeInit.applicationInit(targetSdkVersion, disabledCompatChanges, argv,

            classLoader);

}

findStaticMethod!!!最终调用ActivityThread的main方法,即新进程的入口!

孵化的子进程,command就是静态方法,由于return语句,所以退出了无限循环,返回给caller,退出循环后调用caller.run()。

protected static Runnable applicationInit(int targetSdkVersion, long[] disabledCompatChanges,

        String[] argv, ClassLoader classLoader) {

    // If the application calls System.exit(), terminate the process

    // immediately without running any shutdown hooks.  It is not possible to

    // shutdown an Android application gracefully.  Among other things, the

    // Android runtime shutdown hooks close the Binder driver, which can cause

    // leftover running threads to crash before the process actually exits.

    nativeSetExitWithoutCleanup(true);

    VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);

    VMRuntime.getRuntime().setDisabledCompatChanges(disabledCompatChanges);

    final Arguments args = new Arguments(argv);

    // The end of of the RuntimeInit event (see #zygoteInit).

    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

    // Remaining arguments are passed to the start class's static main

    return findStaticMain(args.startClass, args.startArgs, classLoader);

}

// We're in the child process and have exited the select loop. Proceed to execute the

// command.

if (caller != null) {

    caller.run();

}

一片新天地开启!

Zygote进程呢?zygote进程的处理是返回command,为null,mIsForkChild为false,走如下else分支,只是简单把连接关闭,移除fd,peers,继续无限循环!

if (mIsForkChild) {

    // We're in the child. We should always have a command to run at

    // this stage if processOneCommand hasn't called "exec".

    if (command == null) {

        throw new IllegalStateException("command == null");

    }

    return command;

else {

    // We're in the server - we should never have any commands to run.

    if (command != null) {

        throw new IllegalStateException("command != null");

    }

    // We don't know whether the remote side of the socket was closed or

    // not until we attempt to read from it from processOneCommand. This

    // shows up as a regular POLLIN event in our regular processing

    // loop.

    if (connection.isClosedByPeer()) {

        connection.closeSocket();

        peers.remove(pollIndex);

        socketFDs.remove(pollIndex);

    }

}

27,进入ActivityThread:

来到新启动的用户进程,以下主线程就是所谓的UI线程

public static void main(String[] args) {

    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");

    // Install selective syscall interception

    AndroidOs.install();

    // CloseGuard defaults to true and can be quite spammy.  We

    // disable it here, but selectively enable it later (via

    // StrictMode) on debug builds, but using DropBox, not logs.

    CloseGuard.setEnabled(false);

    Environment.initForCurrentUser();

    // Make sure TrustedCertificateStore looks in the right place for CA certificates

    final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());

    TrustedCertificateStore.setDefaultUserDirectory(configDir);

    // Call per-process mainline module initialization.

    initializeMainlineModules();

    Process.setArgV0("<pre-initialized>");

    // 准备主线程的looper

    Looper.prepareMainLooper();

    // Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.

    // It will be in the format "seq=114"

    long startSeq = 0;

    if (args != null) {

        for (int i = args.length - 1; i >= 0; --i) {

            if (args[i] != null && args[i].startsWith(PROC_START_SEQ_IDENT)) {

                startSeq = Long.parseLong(

                        args[i].substring(PROC_START_SEQ_IDENT.length()));

          

        }

    }

    ActivityThread thread = new ActivityThread();

    // 重要的attach函数,用于绑定上下文,进一步看看

    thread.attach(false, startSeq);

    if (sMainThreadHandler == null) {

        sMainThreadHandler = thread.getHandler();

    }

    if (false) {

        Looper.myLooper().setMessageLogging(new

                LogPrinter(Log.DEBUG, "ActivityThread"));

    }

    // End of event ActivityThreadMain.

    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

    // 开始消息循环

    Looper.loop();

    throw new RuntimeException("Main thread loop unexpectedly exited");

}

attach函数,绑定上下文环境,又一个重要的过程,因为要与AMS通信,建立一些系统关系。

@UnsupportedAppUsage

private void attach(boolean system, long startSeq)

//传入system为false,

//里面又一个binder过程,mgr是AMS,

try {

    // 一个binder,调用ams的attachApplication方法

    mgr.attachApplication(mAppThread, startSeq);

catch (RemoteException ex) {

    throw ex.rethrowFromSystemServer();

}

28,AMS:

来到系统进程

@Override

public final void attachApplication(IApplicationThread thread, long startSeq) {

    // 从客户端binder到此处,绑定上下文,thread为启动的activityThread

    if (thread == null) {

        throw new SecurityException("Invalid application interface");

    }

    synchronized (this) {

        int callingPid = Binder.getCallingPid();

        final int callingUid = Binder.getCallingUid();

        final long origId = Binder.clearCallingIdentity();

        // 进一步看

        attachApplicationLocked(thread, callingPid, callingUid, startSeq);

        Binder.restoreCallingIdentity(origId);

    }

}

进一步看attachApplicationLocked函数
里面这一段,显然,新创建的Activity,进入mAtmInternal.attachApplication中,

// See if the top visible activity is waiting to run in this process...

if (normalMode) {

    try {

        didSomething = mAtmInternal.attachApplication(app.getWindowProcessController());

    catch (Exception e) {

        Slog.wtf(TAG, "Exception thrown launching activities in " + app, e);

        badApp = true;

    }

}

29,ATMS:

没干啥事,委托给RootWindowContainer

@HotPath(caller = HotPath.PROCESS_CHANGE)

@Override

public boolean attachApplication(WindowProcessController wpc) throws RemoteException {

    synchronized (mGlobalLockWithoutBoost) {

        if (Trace.isTagEnabled(TRACE_TAG_WINDOW_MANAGER)) {

            Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "attachApplication:" + wpc.mName);

        }

        try {

            //  继续attach

            return mRootWindowContainer.attachApplication(wpc);

        finally {

            Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);

        }

    }

}

30,RootWindowContainer

boolean attachApplication(WindowProcessController app) throws RemoteException {

    final String processName = app.mName;

    boolean didSomething = false;

    for (int displayNdx = getChildCount() - 1; displayNdx >= 0; --displayNdx) {

        final DisplayContent display = getChildAt(displayNdx);

        final ActivityStack stack = display.getFocusedStack();

        if (stack == null) {

            continue;

        }

        mTmpRemoteException = null;

        mTmpBoolean = false// Set to true if an activity was started.

        final PooledFunction c = PooledLambda.obtainFunction(

                //启动启动   startActivityForAttachedApplicationIfNeeded

                RootWindowContainer::startActivityForAttachedApplicationIfNeeded, this,

                PooledLambda.__(ActivityRecord.class), app, stack.topRunningActivity());

        stack.forAllActivities(c);

        c.recycle();

        if (mTmpRemoteException != null) {

            throw mTmpRemoteException;

        }

        didSomething |= mTmpBoolean;

    }

    if (!didSomething) {

        ensureActivitiesVisible(null0false /* preserve_windows */);

    }

    return didSomething;

}

看到realStartActivityLocked没?

private boolean startActivityForAttachedApplicationIfNeeded(ActivityRecord r,

        WindowProcessController app, ActivityRecord top) {

    if (r.finishing || !r.okToShowLocked() || !r.visibleIgnoringKeyguard || r.app != null

            || app.mUid != r.info.applicationInfo.uid || !app.mName.equals(r.processName)) {

        return false;

    }

    try {

        // 啥都有了 process也有了 该real start了

        if (mStackSupervisor.realStartActivityLocked(r, app, top == r /*andResume*/,

                true /*checkConfig*/)) {

            mTmpBoolean = true;

        }

    catch (RemoteException e) {

        Slog.w(TAG, "Exception in new application when starting activity "

                + top.intent.getComponent().flattenToShortString(), e);

        mTmpRemoteException = e;

        return true;

    }

    return false;

}

31,ActivityStackSupervisor

boolean realStartActivityLocked(ActivityRecord r, WindowProcessController proc,

        boolean andResume, boolean checkConfig) throws RemoteException)

{

...

//一系列启动事宜设置

r.setProcess(proc);

r.launchCount++;

r.lastLaunchTime = SystemClock.uptimeMillis();

proc.setLastActivityLaunchTime(r.lastLaunchTime);

mService.getPackageManagerInternalLocked().notifyPackageUse(

        r.intent.getComponent().getPackageName(), NOTIFY_PACKAGE_USE_ACTIVITY);

r.setSleeping(false);

r.forceNewConfig = false;

mService.getAppWarningsLocked().onStartActivity(r);

r.compat = mService.compatibilityInfoForPackageLocked(r.info.applicationInfo);

// 关键来了,

// Create activity launch transaction.

// 走生命周期

final ClientTransaction clientTransaction = ClientTransaction.obtain(

        proc.getThread(), r.appToken);

final DisplayContent dc = r.getDisplay().mDisplayContent;

//注意,这里设置了启动回调,还记得事务处回调函数的执行吗?

clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),

        System.identityHashCode(r), r.info,

        // TODO: Have this take the merged configuration instead of separate global

        // and override configs.

        mergedConfiguration.getGlobalConfiguration(),

        mergedConfiguration.getOverrideConfiguration(), r.compat,

        r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),

        r.getSavedState(), r.getPersistentSavedState(), results, newIntents,

        dc.isNextTransitionForward(), proc.createProfilerInfoIfNeeded(),

        r.assistToken, r.createFixedRotationAdjustmentsIfNeeded()));

下面这一段,就是启动后下一个生命周期的走向,即resume,

if (andResume) {

    lifecycleItem = ResumeActivityItem.obtain(dc.isNextTransitionForward());

else {

    lifecycleItem = PauseActivityItem.obtain();

}

clientTransaction.setLifecycleStateRequest(lifecycleItem);

// 生命周期开始!

// Schedule transaction.

mService.getLifecycleManager().scheduleTransaction(clientTransaction);

....

}

32,ClientLifecyckeManager

void scheduleTransaction(ClientTransaction transaction) throws RemoteException {

    //获得事务 客户端

    final IApplicationThread client = transaction.getClient();

    //执行

    transaction.schedule();

    if (!(client instanceof Binder)) {

        transaction.recycle();

    }

}

33,来到ClientTransaction

IPC调用,下一步就进入用户进程,也就是刚启动的进程

public void schedule() throws RemoteException {

    //这个this指resumeTransaction,client就是要执行事务的ActivityThread,

    mClient.scheduleTransaction(this);

}

34,回到ActivityThread:

@Override

public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {

    //待resume的activity进来了

    ActivityThread.this.scheduleTransaction(transaction);

}

void scheduleTransaction(ClientTransaction transaction) {

    transaction.preExecute(this);

    sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);

}

然后在Handler中处理,不再赘述

case EXECUTE_TRANSACTION:

    //执行生命周期的事务

    final ClientTransaction transaction = (ClientTransaction) msg.obj;

    //Executor 执行事务

    mTransactionExecutor.execute(transaction);

    if (isSystem()) {

        transaction.recycle();

    }

    // TODO(lifecycler): Recycle locally scheduled transactions.

    break;

35,来到TranslationExecutor:

//执行设置的回调

executeCallbacks(transaction);

//走生命周期

executeLifecycleState(transaction);

mPendingActions.clear();

回忆之前设置的callback哈,先走回调,

@VisibleForTesting

public void executeCallbacks(ClientTransaction transaction) {

    //里面是个循环,简单完成所有回调,

    // launch activity

    // item,就是launchItem,

    item.execute(mTransactionHandler, token, mPendingActions);

    item.postExecute(mTransactionHandler, token, mPendingActions);

}

36,来到LaunchActivityItem:

public void execute(ClientTransactionHandler client, IBinder token,

        PendingTransactionActions pendingActions) {

    Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityStart");

    ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,

            mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,

            mPendingResults, mPendingNewIntents, mIsForward,

            mProfilerInfo, client, mAssistToken, mFixedRotationAdjustments);

    // 进一步 launch

    client.handleLaunchActivity(r, pendingActions, null /* customIntent */);

    Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);

}

37,来到ActivityThread:

@Override

public Activity handleLaunchActivity(ActivityClientRecord r,

        PendingTransactionActions pendingActions, Intent customIntent) {

    // If we are getting ready to gc after going to the background, well

    // we are back active so skip it.

    unscheduleGcIdler();

    mSomeActivitiesChanged = true;

    if (r.profilerInfo != null) {

        mProfiler.setProfiler(r.profilerInfo);

        mProfiler.startProfiling();

    }

    if (r.mPendingFixedRotationAdjustments != null) {

        // The rotation adjustments must be applied before handling configuration, so process

        // level display metrics can be adjusted.

        overrideApplicationDisplayAdjustments(r.token, adjustments ->

                adjustments.setFixedRotationAdjustments(r.mPendingFixedRotationAdjustments));

    }

    // Make sure we are running with the most recent config.

    handleConfigurationChanged(nullnull);

    if (localLOGV) Slog.v(

        TAG, "Handling launch of " + r);

    // Initialize before creating the activity

    if (!ThreadedRenderer.sRendererDisabled

            && (r.activityInfo.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {

        HardwareRenderer.preload();

    }

    WindowManagerGlobal.initialize();

    // Hint the GraphicsEnvironment that an activity is launching on the process.

    GraphicsEnvironment.hintActivityLaunch();

    // 进一步看

    final Activity a = performLaunchActivity(r, customIntent);

    if (a != null) {

        r.createdConfig = new Configuration(mConfiguration);

        reportSizeConfigurations(r);

        if (!r.activity.mFinished && pendingActions != null) {

            pendingActions.setOldState(r.state);

            pendingActions.setRestoreInstanceState(true);

            pendingActions.setCallOnPostCreate(true);

        }

    else {

        // If there was an error, for any reason, tell the activity manager to stop us.

        try {

            ActivityTaskManager.getService()

                    .finishActivity(r.token, Activity.RESULT_CANCELED, null,

                            Activity.DONT_FINISH_TASK_WITH_ACTIVITY);

        catch (RemoteException ex) {

            throw ex.rethrowFromSystemServer();

        }

    }

    return a;

}

看到perfromLaunchActivity没?核心方法!

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {

    ActivityInfo aInfo = r.activityInfo;

    if (r.packageInfo == null) {

        r.packageInfo = getPackageInfo(aInfo.applicationInfo, r.compatInfo,

                Context.CONTEXT_INCLUDE_CODE);

    }

    ComponentName component = r.intent.getComponent();

    if (component == null) {

        component = r.intent.resolveActivity(

            mInitialApplication.getPackageManager());

        r.intent.setComponent(component);

    }

    if (r.activityInfo.targetActivity != null) {

        component = new ComponentName(r.activityInfo.packageName,

                r.activityInfo.targetActivity);

    }

    ContextImpl appContext = createBaseContextForActivity(r);

    Activity activity = null;

    try {

        java.lang.ClassLoader cl = appContext.getClassLoader();

        // 通过反射 new一个activity

        activity = mInstrumentation.newActivity(

                cl, component.getClassName(), r.intent);

        StrictMode.incrementExpectedActivityCount(activity.getClass());

        r.intent.setExtrasClassLoader(cl);

        r.intent.prepareToEnterProcess();

        if (r.state != null) {

            r.state.setClassLoader(cl);

        }

    catch (Exception e) {

        if (!mInstrumentation.onException(activity, e)) {

            throw new RuntimeException(

                "Unable to instantiate activity " + component

                ": " + e.toString(), e);

        }

    }

    try {

        Application app = r.packageInfo.makeApplication(false, mInstrumentation);

        if (localLOGV) Slog.v(TAG, "Performing launch of " + r);

        if (localLOGV) Slog.v(

                TAG, r + ": app=" + app

                ", appName=" + app.getPackageName()

                ", pkg=" + r.packageInfo.getPackageName()

                ", comp=" + r.intent.getComponent().toShortString()

                ", dir=" + r.packageInfo.getAppDir());

        if (activity != null) {

            CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());

            Configuration config = new Configuration(mCompatConfiguration);

            if (r.overrideConfig != null) {

                config.updateFrom(r.overrideConfig);

            }

            if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "

                    + r.activityInfo.name + " with config " + config);

            Window window = null;

            if (r.mPendingRemoveWindow != null && r.mPreserveWindow) {

                window = r.mPendingRemoveWindow;

                r.mPendingRemoveWindow = null;

                r.mPendingRemoveWindowManager = null;

            }

            // Activity resources must be initialized with the same loaders as the

            // application context.

            appContext.getResources().addLoaders(

                    app.getResources().getLoaders().toArray(new ResourcesLoader[0]));

            appContext.setOuterContext(activity);

            // attach函数!

            activity.attach(appContext, this, getInstrumentation(), r.token,

                    r.ident, app, r.intent, r.activityInfo, title, r.parent,

                    r.embeddedID, r.lastNonConfigurationInstances, config,

                    r.referrer, r.voiceInteractor, window, r.configCallback,

                    r.assistToken);

            if (customIntent != null) {

                activity.mIntent = customIntent;

            }

            r.lastNonConfigurationInstances = null;

            checkAndBlockForNetworkAccess();

            activity.mStartedActivity = false;

            int theme = r.activityInfo.getThemeResource();

            if (theme != 0) {

                activity.setTheme(theme);

            }

            activity.mCalled = false;

            if (r.isPersistable()) {

                //回调onCreate

                mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);

            else {

                mInstrumentation.callActivityOnCreate(activity, r.state);

            }

            if (!activity.mCalled) {

                throw new SuperNotCalledException(

                    "Activity " + r.intent.getComponent().toShortString() +

                    " did not call through to super.onCreate()");

            }

            r.activity = activity;

            mLastReportedWindowingMode.put(activity.getActivityToken(),

                    config.windowConfiguration.getWindowingMode());

        }

        r.setState(ON_CREATE);

        // updatePendingActivityConfiguration() reads from mActivities to update

        // ActivityClientRecord which runs in a different thread. Protect modifications to

        // mActivities to avoid race.

        synchronized (mResourcesManager) {

            mActivities.put(r.token, r);

        }

    catch (SuperNotCalledException e) {

        throw e;

    catch (Exception e) {

        if (!mInstrumentation.onException(activity, e)) {

            throw new RuntimeException(

                "Unable to start activity " + component

                ": " + e.toString(), e);

        }

    }

    return activity;

}

这里有很多关键,比如attach函数,如何与window建立联系,再则说view的measure、layout、draw等等 。需单独一章分析。在此处,通过mInstrumentation回调了onCreate方法。

38,来到Instructiontation:

public void callActivityOnCreate(Activity activity, Bundle icicle,

        PersistableBundle persistentState) {

    prePerformCreate(activity);

    activity.performCreate(icicle, persistentState);//create

    postPerformCreate(activity);

}

39,来到Activity:

final void performCreate() {

        dispatchActivityPreCreated(icicle);

        mCanEnterPictureInPicture = true;

// initialize mIsInMultiWindowMode and mIsInPictureInPictureMode before onCreate

        final int windowingMode = getResources().getConfiguration().windowConfiguration

                .getWindowingMode();

        mIsInMultiWindowMode = inMultiWindowMode(windowingMode);

        

        mIsInPictureInPictureMode = windowingMode == WINDOWING_MODE_PINNED;

        restoreHasCurrentPermissionRequest(icicle);

    

        if (persistentState != null) {

            //这个分支主要看是否有保存的持久性数据

            onCreate(icicle, persistentState);//onCreate

        else {

            onCreate(icicle);

        }

        EventLogTags.writeWmOnCreateCalled(mIdent, getComponentName().getClassName(),

         "performCreate");

        

        mActivityTransitionState.readState(icicle);

        mVisibleFromClient = !mWindow.getWindowStyle().getBoolean(

                com.android.internal.R.styleable.Window_windowNoDisplay, false);

        mFragments.dispatchActivityCreated();

        mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());

        dispatchActivityPostCreated(icicle);

    }

注意,走到onCreate,此时window还没与Activity建立联系呢。

@MainThread

@CallSuper

protected void onCreate(@Nullable Bundle savedInstanceState) {

    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " this ": " + savedInstanceState);

   

    if (mLastNonConfigurationInstances != null) {

        mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);

    }

    if (mActivityInfo.parentActivityName != null) {

        if (mActionBar == null) {

            mEnableDefaultActionBarUp = true;

        else {

            mActionBar.setDefaultDisplayHomeAsUpEnabled(true);

        }

    }

    if (savedInstanceState != null) {

        mAutoFillResetNeeded = savedInstanceState.getBoolean(AUTOFILL_RESET_NEEDED, false);

        mLastAutofillId = savedInstanceState.getInt(LAST_AUTOFILL_ID,

                View.LAST_APP_AUTOFILL_ID);

        if (mAutoFillResetNeeded) {

            getAutofillManager().onCreate(savedInstanceState);

        }

        Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);

        mFragments.restoreAllState(p, mLastNonConfigurationInstances != null

                ? mLastNonConfigurationInstances.fragments : null);

    }

    mFragments.dispatchCreate();

    dispatchActivityCreated(savedInstanceState);

    if (mVoiceInteractor != null) {

        mVoiceInteractor.attachActivity(this);

    }

    mRestoredFromBundle = savedInstanceState != null;

    mCalled = true;

}

40,回到TransactionExecutor:

//执行设置的回调

executeCallbacks(transaction);

//走生命周期

executeLifecycleState(transaction);

executeCallbacks执行完毕,完成了onCreate过程,执行executeLifecycleState函数,

/** Transition to the final state if requested by the transaction. 如果事务请求,则转换到最终状态。*/

private void executeLifecycleState(ClientTransaction transaction) {

    

    final ActivityLifecycleItem lifecycleItem = transaction.getLifecycleStateRequest();  //这个lifecycleItem是resume,之前已经设置过,

    if (lifecycleItem == null) {

        // No lifecycle request, return early.

        return;

    }

    final IBinder token = transaction.getActivityToken();

    final ActivityClientRecord r = mTransactionHandler.getActivityClient(token);

    if (DEBUG_RESOLVER) {

        Slog.d(TAG, tId(transaction) + "Resolving lifecycle state: "

                + lifecycleItem + " for activity: "

                + getShortActivityName(token, mTransactionHandler));

    }

    if (r == null) {

        // Ignore requests for non-existent client records for now.

        return;

    }

    // Cycle to the state right before the final requested state.

    // onCreate之后,走start 走resume,

    cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);

    // 第二次调用:activity的resumeItem

    lifecycleItem.execute(mTransactionHandler, token, mPendingActions);

    lifecycleItem.postExecute(mTransactionHandler, token, mPendingActions);

}

确定下一个周期,来到cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);

下一个状态在TransactionExecutorHelper中确定,

@VisibleForTesting

public IntArray getLifecyclePath(int start, int finish, boolean excludeLastState) {

     //参数start肯定是 ON_CREATE,finish是ON_RESUME,

    if (start == UNDEFINED || finish == UNDEFINED) {

        throw new IllegalArgumentException("Can't resolve lifecycle path for undefined state");

    }

    if (start == ON_RESTART || finish == ON_RESTART) {

        throw new IllegalArgumentException(

                "Can't start or finish in intermittent RESTART state");

    }

    if (finish == PRE_ON_CREATE && start != finish) {

        throw new IllegalArgumentException("Can only start in pre-onCreate state");

    }

    mLifecycleSequence.clear();

    if (finish >= start) {

        if (start == ON_START && finish == ON_STOP) {

            // A case when we from start to stop state soon, we don't need to go

            // through the resumed, paused state.

            mLifecycleSequence.add(ON_STOP);

        else {

            // just go there ,根据参数,把应该走的序列都添加进去,分别是start、resume,

            for (int i = start + 1; i <= finish; i++) {

                mLifecycleSequence.add(i);

            }

        }

    else // finish < start, can't just cycle down

        if (start == ON_PAUSE && finish == ON_RESUME) {

            // Special case when we can just directly go to resumed state.

            mLifecycleSequence.add(ON_RESUME);

        else if (start <= ON_STOP && finish >= ON_START) {

            // Restart and go to required state.

            // Go to stopped state first.

            for (int i = start + 1; i <= ON_STOP; i++) {

                mLifecycleSequence.add(i);

            }

            // Restart

            mLifecycleSequence.add(ON_RESTART);

            // Go to required state

            for (int i = ON_START; i <= finish; i++) {

                mLifecycleSequence.add(i);

            }

        else {

            // Relaunch and go to required state

            // Go to destroyed state first.

            for (int i = start + 1; i <= ON_DESTROY; i++) {

                mLifecycleSequence.add(i);

            }

            // Go to required state

            for (int i = ON_CREATE; i <= finish; i++) {

                mLifecycleSequence.add(i);

            }

        }

    }

    // Remove last transition in case we want to perform it with some specific params.

    if (excludeLastState && mLifecycleSequence.size() != 0) {

        mLifecycleSequence.remove(mLifecycleSequence.size() - 1);

    }

    return mLifecycleSequence;

}

返回后,根据拿到的周期序列,在performLifecycleSequence中处理,调试后拿到的是2,也就是执行start。
执行handleStartActivity

/** Transition the client through previously initialized state sequence. */

private void performLifecycleSequence(ActivityClientRecord r, IntArray path,

        ClientTransaction transaction) {

    final int size = path.size();

    for (int i = 0, state; i < size; i++) {

        state = path.get(i);

        if (DEBUG_RESOLVER) {

            Slog.d(TAG, tId(transaction) + "Transitioning activity: "

                    + getShortActivityName(r.token, mTransactionHandler)

                    " to state: " + getStateName(state));

        }

        switch (state) {

            case ON_CREATE:

                mTransactionHandler.handleLaunchActivity(r, mPendingActions,

                        null /* customIntent */);

                break;

            case ON_START:

                mTransactionHandler.handleStartActivity(r.token, mPendingActions);

                break;

            case ON_RESUME:

                mTransactionHandler.handleResumeActivity(r.token, false /* finalStateRequest */,

                        r.isForward, "LIFECYCLER_RESUME_ACTIVITY");

                break;

            case ON_PAUSE:

                mTransactionHandler.handlePauseActivity(r.token, false /* finished */,

                        false /* userLeaving */0 /* configChanges */, mPendingActions,

                        "LIFECYCLER_PAUSE_ACTIVITY");

                break;

            case ON_STOP:

                mTransactionHandler.handleStopActivity(r.token, 0 /* configChanges */,

                        mPendingActions, false /* finalStateRequest */,

                        "LIFECYCLER_STOP_ACTIVITY");

                break;

            case ON_DESTROY:

                mTransactionHandler.handleDestroyActivity(r.token, false /* finishing */,

                        0 /* configChanges */false /* getNonConfigInstance */,

                        "performLifecycleSequence. cycling to:" + path.get(size - 1));

                break;

            case ON_RESTART:

                mTransactionHandler.performRestartActivity(r.token, false /* start */);

                break;

            default:

                throw new IllegalArgumentException("Unexpected lifecycle state: " + state);

        }

    }

}

41,回到ActivityThread:

@Override

public void handleStartActivity(IBinder token, PendingTransactionActions pendingActions) {

    final ActivityClientRecord r = mActivities.get(token);

    final Activity activity = r.activity;

    if (r.activity == null) {

        // TODO(lifecycler): What do we do in this case?

        return;

    }

    if (!r.stopped) {

        throw new IllegalStateException("Can't start activity that is not stopped.");

    }

    if (r.activity.mFinished) {

        // TODO(lifecycler): How can this happen?

        return;

    }

    unscheduleGcIdler();

    // Start周期

    activity.performStart("handleStartActivity");

    r.setState(ON_START);

    if (pendingActions == null) {

        // No more work to do.

        return;

    }

    // Restore instance state

    if (pendingActions.shouldRestoreInstanceState()) {

        if (r.isPersistable()) {

            if (r.state != null || r.persistentState != null) {

                mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state,

                        r.persistentState);

            }

        else if (r.state != null) {

            mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);

        }

    }

    // Call postOnCreate()

    if (pendingActions.shouldCallOnPostCreate()) {

        activity.mCalled = false;

        if (r.isPersistable()) {

            mInstrumentation.callActivityOnPostCreate(activity, r.state,

                    r.persistentState);

        else {

            mInstrumentation.callActivityOnPostCreate(activity, r.state);

        }

        if (!activity.mCalled) {

            throw new SuperNotCalledException(

                    "Activity " + r.intent.getComponent().toShortString()

                            " did not call through to super.onPostCreate()");

        }

    }

    updateVisibility(r, true /* show */);

    mSomeActivitiesChanged = true;

}

// 在Activity中执行哦

final void performStart(String reason) {

    dispatchActivityPreStarted();

    mActivityTransitionState.setEnterActivityOptions(this, getActivityOptions());

    mFragments.noteStateNotSaved();

    mCalled = false;

    mFragments.execPendingActions();

    long startTime = SystemClock.uptimeMillis();

    mInstrumentation.callActivityOnStart(this);//onStart

    //EventLogTags.writeWmOnStartCalled(mIdent, getComponentName().getClassName(), reason);

    long took = SystemClock.uptimeMillis() - startTime;

    writeEventLog(LOG_AM_ON_START_CALLED, reason, took);

    checkLifecycleTime(took, SLOW_ON_START_THRESHOLD_MS, "onStart");

    mInstrumentation.callActivityOnStart(this);//onCreate

    if (!mCalled) {

        throw new SuperNotCalledException(

            "Activity " + mComponent.toShortString() +

            " did not call through to super.onStart()");

    }

    mFragments.dispatchStart();

    mFragments.reportLoaderStart();

    // Warn app developers if the dynamic linker logged anything during startup.

    boolean isAppDebuggable =

            (mApplication.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;

    if (isAppDebuggable) {

        String dlwarning = getDlWarning();

        if (dlwarning != null) {

            String appName = getApplicationInfo().loadLabel(getPackageManager())

                    .toString();

            String warning = "Detected problems with app native libraries\n" +

                             "(please consult log for detail):\n" + dlwarning;

            if (isAppDebuggable) {

                  new AlertDialog.Builder(this).

                      setTitle(appName).

                      setMessage(warning).

                      setPositiveButton(android.R.string.ok, null).

                      setCancelable(false).

                      show();

            else {

                Toast.makeText(this, appName + "\n" + warning, Toast.LENGTH_LONG).show();

            }

        }

    }

    GraphicsEnvironment.getInstance().showAngleInUseDialogBox(this);

    mActivityTransitionState.enterReady(this);

    dispatchActivityPostStarted();

}

/**

 * Perform calling of an activity's {@link Activity#onStart}

 * method.  The default implementation simply calls through to that method.

 *

 * @param activity The activity being started.

 */

public void callActivityOnStart(Activity activity) {

    activity.onStart();

}

42,回到ActivityThread,继续Resume:

//在此处才看到和view相关的东西出现,另一章重点讨论此处。

@Override

public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,

        String reason) {

    // If we are getting ready to gc after going to the background, well

    // we are back active so skip it.

    unscheduleGcIdler();

    mSomeActivitiesChanged = true;

    // TODO Push resumeArgs into the activity for consideration

    final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason);

    if (r == null) {

        // We didn't actually resume the activity, so skipping any follow-up actions.

        return;

    }

    if (mActivitiesToBeDestroyed.containsKey(token)) {

        // Although the activity is resumed, it is going to be destroyed. So the following

        // UI operations are unnecessary and also prevents exception because its token may

        // be gone that window manager cannot recognize it. All necessary cleanup actions

        // performed below will be done while handling destruction.

        return;

    }

    final Activity a = r.activity;

    if (localLOGV) {

        Slog.v(TAG, "Resume " + r + " started activity: " + a.mStartedActivity

                ", hideForNow: " + r.hideForNow + ", finished: " + a.mFinished);

    }

    final int forwardBit = isForward

            ? WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;

    // If the window hasn't yet been added to the window manager,

    // and this guy didn't finish itself or start another activity,

    // then go ahead and add the window.

    boolean willBeVisible = !a.mStartedActivity;

    if (!willBeVisible) {

        try {

            willBeVisible = ActivityTaskManager.getService().willActivityBeVisible(

                    a.getActivityToken());

        catch (RemoteException e) {

            throw e.rethrowFromSystemServer();

        }

    }

    if (r.window == null && !a.mFinished && willBeVisible) {

        r.window = r.activity.getWindow();

        View decor = r.window.getDecorView();

        decor.setVisibility(View.INVISIBLE);

        ViewManager wm = a.getWindowManager();

        WindowManager.LayoutParams l = r.window.getAttributes();

        a.mDecor = decor;

        l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;

        l.softInputMode |= forwardBit;

        if (r.mPreserveWindow) {

            a.mWindowAdded = true;

            r.mPreserveWindow = false;

            // Normally the ViewRoot sets up callbacks with the Activity

            // in addView->ViewRootImpl#setView. If we are instead reusing

            // the decor view we have to notify the view root that the

            // callbacks may have changed.

            ViewRootImpl impl = decor.getViewRootImpl();

            if (impl != null) {

                impl.notifyChildRebuilt();

            }

        }

        if (a.mVisibleFromClient) {

            if (!a.mWindowAdded) {

                a.mWindowAdded = true;

                wm.addView(decor, l);

            else {

                // The activity will get a callback for this {@link LayoutParams} change

                // earlier. However, at that time the decor will not be set (this is set

                // in this method), so no action will be taken. This call ensures the

                // callback occurs with the decor set.

                a.onWindowAttributesChanged(l);

            }

        }

        // If the window has already been added, but during resume

        // we started another activity, then don't yet make the

        // window visible.

    else if (!willBeVisible) {

        if (localLOGV) Slog.v(TAG, "Launch " + r + " mStartedActivity set");

        r.hideForNow = true;

    }

    // Get rid of anything left hanging around.

    cleanUpPendingRemoveWindows(r, false /* force */);

    // The window is now visible if it has been added, we are not

    // simply finishing, and we are not starting another activity.

    if (!r.activity.mFinished && willBeVisible && r.activity.mDecor != null && !r.hideForNow) {

        if (r.newConfig != null) {

            performConfigurationChangedForActivity(r, r.newConfig);

            if (DEBUG_CONFIGURATION) {

                Slog.v(TAG, "Resuming activity " + r.activityInfo.name + " with newConfig "

                        + r.activity.mCurrentConfig);

            }

            r.newConfig = null;

        }

        if (localLOGV) Slog.v(TAG, "Resuming " + r + " with isForward=" + isForward);

        ViewRootImpl impl = r.window.getDecorView().getViewRootImpl();

        WindowManager.LayoutParams l = impl != null

                ? impl.mWindowAttributes : r.window.getAttributes();

        if ((l.softInputMode

                & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION)

                != forwardBit) {

            l.softInputMode = (l.softInputMode

                    & (~WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION))

                    | forwardBit;

            if (r.activity.mVisibleFromClient) {

                ViewManager wm = a.getWindowManager();

                View decor = r.window.getDecorView();

                wm.updateViewLayout(decor, l);

            }

        }

        r.activity.mVisibleFromServer = true;

        mNumVisibleActivities++;

        if (r.activity.mVisibleFromClient) {

            r.activity.makeVisible();

        }

    }

    r.nextIdle = mNewActivities;

    mNewActivities = r;

    if (localLOGV) Slog.v(TAG, "Scheduling idle handler for " + r);

    Looper.myQueue().addIdleHandler(new Idler());

}

@VisibleForTesting

public ActivityClientRecord performResumeActivity(IBinder token, boolean finalStateRequest,

        String reason) {

    final ActivityClientRecord r = mActivities.get(token);

    if (localLOGV) {

        Slog.v(TAG, "Performing resume of " + r + " finished=" + r.activity.mFinished);

    }

    if (r == null || r.activity.mFinished) {

        return null;

    }

    if (r.getLifecycleState() == ON_RESUME) {

        if (!finalStateRequest) {

            final RuntimeException e = new IllegalStateException(

                    "Trying to resume activity which is already resumed");

            Slog.e(TAG, e.getMessage(), e);

            Slog.e(TAG, r.getStateString());

            // TODO(lifecycler): A double resume request is possible when an activity

            // receives two consequent transactions with relaunch requests and "resumed"

            // final state requests and the second relaunch is omitted. We still try to

            // handle two resume requests for the final state. For cases other than this

            // one, we don't expect it to happen.

        }

        return null;

    }

    if (finalStateRequest) {

        r.hideForNow = false;

        r.activity.mStartedActivity = false;

    }

    try {

        r.activity.onStateNotSaved();

        r.activity.mFragments.noteStateNotSaved();

        checkAndBlockForNetworkAccess();

        if (r.pendingIntents != null) {

            deliverNewIntents(r, r.pendingIntents);

            r.pendingIntents = null;

        }

        if (r.pendingResults != null) {

            deliverResults(r, r.pendingResults, reason);

            r.pendingResults = null;

        }

        r.activity.performResume(r.startsNotResumed, reason);

        r.state = null;

        r.persistentState = null;

        r.setState(ON_RESUME);

        reportTopResumedActivityChanged(r, r.isTopResumedActivity, "topWhenResuming");

    catch (Exception e) {

        if (!mInstrumentation.onException(r.activity, e)) {

            throw new RuntimeException("Unable to resume activity "

                    + r.intent.getComponent().toShortString() + ": " + e.toString(), e);

        }

    }

    return r;

}

//Activity

final void performResume(boolean followedByPause, String reason) {

    dispatchActivityPreResumed();

    performRestart(true /* start */, reason);

    mFragments.execPendingActions();

    mLastNonConfigurationInstances = null;

    if (mAutoFillResetNeeded) {

        // When Activity is destroyed in paused state, and relaunch activity, there will be

        // extra onResume and onPause event,  ignore the first onResume and onPause.

        // see ActivityThread.handleRelaunchActivity()

        mAutoFillIgnoreFirstResumePause = followedByPause;

        if (mAutoFillIgnoreFirstResumePause && DEBUG_LIFECYCLE) {

            Slog.v(TAG, "autofill will ignore first pause when relaunching " this);

        }

    }

    mCalled = false;

    // mResumed is set by the instrumentation

    if (!mCalled) {

        throw new SuperNotCalledException(

            "Activity " + mComponent.toShortString() +

            " did not call through to super.onResume()");

    }

    // invisible activities must be finished before onResume() completes

    if (!mVisibleFromClient && !mFinished) {

        Log.w(TAG, "An activity without a UI must call finish() before onResume() completes");

        if (getApplicationInfo().targetSdkVersion

                > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {

            throw new IllegalStateException(

                    "Activity " + mComponent.toShortString() +

                    " did not call finish() prior to onResume() completing");

        }

    }

    // Now really resume, and install the current status bar and menu.

    mCalled = false;

    mFragments.dispatchResume();

    mFragments.execPendingActions();

    onPostResume();

    if (!mCalled) {

        throw new SuperNotCalledException(

            "Activity " + mComponent.toShortString() +

            " did not call through to super.onPostResume()");

    }

    dispatchActivityPostResumed();

}

//Instructiontation

public void callActivityOnResume(Activity activity) {

    activity.mResumed = true;

    activity.onResume();

     

    if (mActivityMonitors != null) {

        synchronized (mSync) {

            final int N = mActivityMonitors.size();

            for (int i=0; i<N; i++) {

                final ActivityMonitor am = mActivityMonitors.get(i);

                am.match(activity, activity, activity.getIntent());

            }

        }

    }

}

//Activity

@CallSuper

protected void onResume() {

    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onResume " this);

    dispatchActivityResumed();

    mActivityTransitionState.onResume(this);

    enableAutofillCompatibilityIfNeeded();

    if (mAutoFillResetNeeded) {

        if (!mAutoFillIgnoreFirstResumePause) {

            View focus = getCurrentFocus();

            if (focus != null && focus.canNotifyAutofillEnterExitEvent()) {

                // TODO(b/148815880): Bring up keyboard if resumed from inline authentication.

                // TODO: in Activity killed/recreated case, i.e. SessionLifecycleTest#

                // testDatasetVisibleWhileAutofilledAppIsLifecycled: the View's initial

                // window visibility after recreation is INVISIBLE in onResume() and next frame

                // ViewRootImpl.performTraversals() changes window visibility to VISIBLE.

                // So we cannot call View.notifyEnterOrExited() which will do nothing

                // when View.isVisibleToUser() is false.

                getAutofillManager().notifyViewEntered(focus);

            }

        }

    }

    notifyContentCaptureManagerIfNeeded(CONTENT_CAPTURE_RESUME);

    mCalled = true;

   

}

43,回到ActivityThread:
大结局,之前的所有步骤哈,肯定都在此循环中进行,因为Zygote产生的如下子进程已经进入无限循环,接收消息了。

在前面启动Activity过程中,不是有许多handleTransaction、handleLaunchActivity等等许多函数嘛,其实就在此Handler中执行。

至此,Activity详细启动流程,完毕。

thread.attach(false, startSeq);

if (sMainThreadHandler == null) {

sMainThreadHandler = thread.getHandler();

}

if (false) {

Looper.myLooper().setMessageLogging(new

LogPrinter(Log.DEBUG, "ActivityThread"));

}

// End of event ActivityThreadMain.

Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);

// 开始消息循环

Looper.loop();

throw new RuntimeException("Main thread loop unexpectedly exited");

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

关于一些回调的细节:

首先,在ActivityThread中handleLaunchActivity时,会将反射产生的Activity与系统上下文经历联系,主要在Activity的attach函数中,

@UnsupportedAppUsage

    final void attach(Context context, ActivityThread aThread,

            Instrumentation instr, IBinder token, int ident,

            Application application, Intent intent, ActivityInfo info,

            CharSequence title, Activity parent, String id,

            NonConfigurationInstances lastNonConfigurationInstances,

            Configuration config, String referrer, IVoiceInteractor voiceInteractor,

            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {

        attachBaseContext(context);

        mFragments.attachHost(null /*parent*/);

        mWindow = new PhoneWindow(this, window, activityConfigCallback); //这就是窗口具体实现类,PhoneWindow

        mWindow.setWindowControllerCallback(mWindowControllerCallback);

        mWindow.setCallback(this);//窗口事件回调对象设置为this,需要在Activity中重写对应方法

        mWindow.setOnWindowDismissedCallback(this);

        mWindow.getLayoutInflater().setPrivateFactory(this);

        if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {

            mWindow.setSoftInputMode(info.softInputMode);

        }

        if (info.uiOptions != 0) {

            mWindow.setUiOptions(info.uiOptions);

        }

        mUiThread = Thread.currentThread();

        //上下文变量的保存

        mMainThread = aThread;

        mInstrumentation = instr;

        mToken = token;

        mAssistToken = assistToken;

        mIdent = ident;

        mApplication = application;

        mIntent = intent;

        mReferrer = referrer;

        mComponent = intent.getComponent();

        mActivityInfo = info;

        mTitle = title;

        mParent = parent;

        mEmbeddedID = id;

        mLastNonConfigurationInstances = lastNonConfigurationInstances;

        if (voiceInteractor != null) {

            if (lastNonConfigurationInstances != null) {

                mVoiceInteractor = lastNonConfigurationInstances.voiceInteractor;

            else {

                mVoiceInteractor = new VoiceInteractor(voiceInteractor, thisthis,

                        Looper.myLooper());

            }

        }

        // 保存WindowManager

        mWindow.setWindowManager(

                (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),

                mToken, mComponent.flattenToString(),

                (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);

        if (mParent != null) {

            mWindow.setContainer(mParent.getWindow());

        }

        mWindowManager = mWindow.getWindowManager();

        mCurrentConfig = config;

        mWindow.setColorMode(info.colorMode);

        mWindow.setPreferMinimalPostProcessing(

                (info.flags & ActivityInfo.FLAG_PREFER_MINIMAL_POST_PROCESSING) != 0);

        setAutofillOptions(application.getAutofillOptions());

        setContentCaptureOptions(application.getContentCaptureOptions());

    }

最近列表的添加,在ActivityStart的startActivityInner函数末尾

mSupervisor.mRecentTasks.add(mStartActivity.getTask());

//最近列表维护是由ActivityStackSupervisor维护,即所有栈的超管

mSupervisor.handleNonResizableTaskIfNeeded(mStartActivity.getTask(),

mPreferredWindowingMode, mPreferredTaskDisplayArea, mTargetStack);

return START_SUCCESS;

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值