从上面的源码,我们大概可以看出mAttachInfo
字段在这里比较关键,当其有值时,其实和普通的Handler.post
就没区别了,但有时它是没值的,比如我们上面示例代码里的onCreate阶段,那么这时执行到了getRunQueue().post(action);
这行代码,从这段注释也大概可以看出来真正的执行会被延迟(这里的Postpone
注释);我们接着往下看看getRunQueue相关的代码,如下:
/** 其实这段注释已经说的很清楚明了了!!!
* Queue of pending runnables. Used to postpone calls to post() until this
* view is attached and has a handler.
*/
private HandlerActionQueue mRunQueue;
private HandlerActionQueue getRunQueue() {
if (mRunQueue == null) {
mRunQueue = new HandlerActionQueue();
}
return mRunQueue;
}
从上面我们可以看出,mRunQueue就是View用来处理它还没attach到window(还没对应的handler)时,客户代码发起的post调用的,起了一个临时缓存
的作用。不然总不能丢弃吧,这样开发体验就太差了!!!
紧接着,我们继续看下HandlerAct