Activity启动过程

Activity启动过程

Activity

1.startActivity
2.startActivityForResult

Instrumentation

3.execStartActivity

ActivityManagerService

4.startActivity
5.startActivityAsUser

ActivityStarter

6.startActivityMayWait

解析出Intent相匹配的Activityinfo
得到启动Activity的Task

7.startActivityLocked

禁止App切换恢复
校验参数

8.startActivityUnchecked
ActivityStackSupervisor
9.resumeFocusedStackTopActivityLocked

ActivityStack

10.resumeTopActivityUncheckedLocked
11.resumeTopActivityInnerLocked

判断是否又resume的Activity有则暂停
唤醒目标ActivityActivity或重启Acitivity

ActivityStackSupervisor

12.startSpecificActivityLocked

没有启动app的,通过zygote创建应用进程startProcessLocked
启动了了进入14步启动realStartActivityLocked
ActivityManagerService

13.startProcessLocked

ActivityStackSupervisor

14.realStartActivityLocked

通过发送事务执行事务 最后执行LaunchActivityItem 和ResumeActivityItem 的execute
最终调用activityThread.handleLaunchActivity, activityThread.handleResumeActivity
handleLaunchActivity 会调用performLaunchActivity
mInstrumentation.newActivity 创建acitivty
activity.attach 创建phonewindow
mInstrumentation.callActivityOnCreate
调用created 调用onCreated 中window.setContentView生成dectoView

handleResumeActivity
WindowManagerGlobal把dectoView添加到window addView 会生ViewRootImpl
scheduleTraversals performTraversals performMeasure performLayout performDraw view绘画工作机制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create activity launch transaction.
final ClientTransaction clientTransaction = ClientTransaction.obtain(app.thread,
r.appToken);
clientTransaction.addCallback(LaunchActivityItem.obtain(new Intent(r.intent),
...
if (andResume) {
lifecycleItem = ResumeActivityItem.obtain(mService.isNextTransitionForward());
} else {
lifecycleItem = PauseActivityItem.obtain();
}
clientTransaction.setLifecycleStateRequest(lifecycleItem);

// Schedule transaction.
mService.getLifecycleManager().scheduleTransaction(clientTransaction);

ActivityThread.this.scheduleTransaction

ActivityThread 的父类

1
2
3
4
5
6
7
8
public abstract class ClientTransactionHandler {
// Schedule phase related logic and handlers.

/** Prepare and schedule transaction for execution. */
void scheduleTransaction(ClientTransaction transaction) {
transaction.preExecute(this);
sendMessage(ActivityThread.H.EXECUTE_TRANSACTION, transaction);
}

ActivityThread H 中

1
2
3
4
5
6
7
8
9
10
case EXECUTE_TRANSACTION:
final ClientTransaction transaction = (ClientTransaction) msg.obj;
mTransactionExecutor.execute(transaction);//最后执行LaunchActivityItem 和PauseActivityItem 等里面的execute方法
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.

LaunchActivityItem 中

1
2
3
4
5
6
7
8
9
10
11
@Override
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);
client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
}

Alt text

Alt text