本文探查的是,从App冷启动后到MainActivity生命周期切换的系统实现。
调试步骤
- 在com.android.server.wm.RootWindowContainer#attachApplication 方法下断点,为了attach目标进程
- 在com.android.server.wm.ActivityTaskSupervisor#realStartActivityLocked 方法下断点,为了进入‘3’
- 在com.android.server.wm.ClientLifecycleManager#scheduleTransactionAndLifecycleItems(
- . android.app.IApplicationThread, // 目标进程: IApplicationThread S t u b Stub StubProxy
- android.app.servertransaction.ClientTransactionItem, // LauncherActivityItem
- android.app.servertransaction.ActivityLifecycleItem, boolean) // ResumeActivityItem
- 在ActivityThread H H HhandleMessage的EXECUTE_TRANSACTION判断处断点,所有的ClientTransaction都可以从这里查看
- 可以看到 TopResumedActivityChangeItem,WindowStateResizeItem等ActivityTransactionItem的子类实现
- 也可看到 LaunchActivityItem ,这是ClientTransactionItem的子类
- 也可看到 ResumeActivityItem,等生命周期,这些是ActivityLifeCycleItem的子类,ActivityTransactionItem的孙子类
- 在android.app.servertransaction.TransactionExecutor#executeLifecycleItem 里下断点,看代码:
// Cycle to the state right before the final requested state. 进入最终状态(比如:resume)前,执行的ClientTransatction(即:start)
cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */, transaction);// Execute the final transition with proper parameters.
lifecycleItem.execute(mTransactionHandler, mPendingActions);
lifecycleItem.postExecute(mTransactionHandler, mPendingActions);
- android.app.servertransaction.TransactionExecutorHelper#getLifecyclePath
最终在这里,看到生命周期的转换关系。
ClientTransaction相关类图
/aosp15/frameworks/base/core/java/android/app/servertransaction
ClienTransaction
注意这两个属性
/*** List of transaction items that should be executed in order. Including both* {@link ActivityLifecycleItem} and other {@link ClientTransactionItem}.* ClientTransaction列表,顺序执行*/
@Nullable
private List<ClientTransactionItem> mTransactionItems;
/*** Final lifecycle state in which the client activity should be after the transaction is executed.* activity的最终生命周期状态*/
@Nullable
private ActivityLifecycleItem mLifecycleStateRequest;