我们都知道Activity的生命周期onRestart第一次启动Activity不会执行,当Activity执行过onStop之后,再恢复页面时会执行onRestart,从源码Android9.0看它时怎么实现的
Activity.java
final void performResume(boolean followedByPause, String reason) {
performRestart(true /* start */, reason);
...
mInstrumentation.callActivityOnResume(this);
...
}
final void performRestart(boolean start, String reason) {
...
//mStopped 只有执行过performStop才会设置成true,所以第一次不会执行
if (mStopped) {
...
//start 传过来的是true,一定会会执行,但是第一次启动Activity走不是这里
if (start) {
performStart(reason);
}
}
}
第一次onStart是在哪里调用的呢?
AMS发送过来的事务ClientTransaction,在TransactionExecutor中执行
//TransactionExecutor.java
public void execute(ClientTransaction transaction) {
executeCallbacks(transaction);
executeLifecycleState(transaction);
}
private void executeLifecycleState(ClientTransaction transaction) {
...
cycleToPath(r, lifecycleItem.getTargetState(), true /* excludeLastState */);
...
}
private void cycleToPath(ActivityClientRecord r, int finish,
boolean excludeLastState) {
//该start == on_create = 1
final int start = r.getLifecycleState();
//finish == on_resume = 3
final IntArray path = mHelper.getLifecyclePath(start, finish, excludeLastState);
performLifecycleSequence(r, path);
}
public IntArray getLifecyclePath(int start, int finish, boolean excludeLastState) {
...
if (finish >= start) { // 3 >= 1
for (int i = start + 1; i <= finish; i++) {
// on_start, on_resume, on_restart = 7不在这里
mLifecycleSequence.add(i);
}
}
...
if (excludeLastState && mLifecycleSequence.size() != 0) {
//把on_resume又删除了
mLifecycleSequence.remove(mLifecycleSequence.size() - 1);
}
}
private void performLifecycleSequence(ActivityClientRecord r, IntArray path) {
...
switch(state) {
case ON_START:
//这个mTransactionHandler就是ActivityThread
mTransactionHandler.handleStartActivity(r, mPendingActions);
break;
}
}
这里就是Activity第一次启动时onStart被调用的地方