Android 面试总结 - View,4面阿里拿到P7Offer

}

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 在什么时候创建的


ViewRootImpl 是我们 View 体系中非常重要的类,其中 绘制、布局、测量,刷新布局等等都是在它内部发起的,而且 Activity 是和 ViewRootImpl 一一对应的,也就是一个 Activity 都有一个 ViewRootImpl 对象。具体我们在这里不在延伸了。

我们知道 Activity 的启动流程会走 ActivityThread 的 performLaunchActivity 方法,其中会调用到 Activity 的 onCreate,同样 onStart 会在 ActivityThread 的 performStart 方法调用,onResume 会在 performResumeActivity 中调用。看下这三个生命周期方法 debug 截图。

onCreate.png

onStart.png

onResume.png

在这里只关注 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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值