}
public void executeActions(Handler handler) {
synchronized (this) {
final HandlerAction[] actions = mActions;
for (int i = 0, count = mCount; i < count; i++) {
final HandlerAction handlerAction = actions[i];
handler.postDelayed(handlerAction.action, handlerAction.delay);
}
mActions = null;
mCount = 0;
}
}
private static class HandlerAction {
final Runnable action;
final long delay;
public HandlerAction(Runnable action, long delay) {
this.action = action;
this.delay = delay;
}
public boolean matches(Runnable otherAction) {
return otherAction == null && action == null
|| action != null && action.equals(otherAction);
}
}
}
HandlerActionQueue 内部维护了一个初试大小为 4 的数组,HandlerAction 是 Runnable 对象和 delay 延时时间的一个包装类。HandlerActionQueue 的 post 方法调用了 postDelayed 传入了 Runnable 对象,第二个参数延时时间固定为 0,postDelayed 方法将 Runnable 对象和延时时间包装为 HandlerAction 对象并添加到数组中。
到这儿,View.post 方法就走完了。上面一会儿梳理下 ViewRootImpl 在什么时候创建的,现在来看下。
ViewRootImpl 是我们 View 体系中非常重要的类,其中 绘制、布局、测量,刷新布局等等都是在它内部发起的,而且 Activity 是和 ViewRootImpl 一一对应的,也就是一个 Activity 都有一个 ViewRootImpl 对象。具体我们在这里不在延伸了。
我们知道 Activity 的启动流程会走 ActivityThread 的 performLaunchActivity 方法,其中会调用到 Activity 的 onCreate,同样 onStart 会在 ActivityThread 的 performStart 方法调用,onResume 会在 performResumeActivity 中调用。看下这三个生命周期方法 debug 截图。
在这里只关注 onResume,从 ActivityThread 的 performResumeActivity 方法开始:
// android.app.ActivityThread
public final class ActivityThread extends ClientTransactionHandler {
@Override
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
…
// TODO Push resumeArgs into the activity for consideration
// 1
final ActivityClientRecord r = performResumeActivity(token, finalStateRequest, reason);
…
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;