欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android从源码分析handler.post(runnable),view.post(runnable),runOnUiThread(runnable)执行时机

程序员文章站 2022-03-27 11:48:02
参考: https://blog.csdn.net/fengshenlangzi/article/details/51727264?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2...

参考: https://blog.csdn.net/fengshenlangzi/article/details/51727264?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

view/activity的生命周期关系

在MyActivity中布局添加一个CustomView,然后在各个回调中添加log,会有如下调用顺序:

E/MyActivity: onCreate: 
E/CustomView: onFinishInflate: 
E/MyActivity: onResume: 
E/MyActivity: onAttachedToWindow: 
E/CustomView: onAttachedToWindow: 
E/CustomView: onMeasure: 
E/CustomView: onMeasure: 
E/CustomView: onMeasure: 
E/CustomView: onSizeChanged: 
E/CustomView: onLayout: 
E/CustomView: onDraw: 
E/MainActivity: onWindowFocusChanged: true
E/CustomView: onWindowFocusChanged: true

//接下来退出Activity
E/MainActivity: onWindowFocusChanged: false
E/CustomView: onWindowFocusChanged: false
E/MyActivity: onDestroy: 
E/CustomView: onDetachedFromWindow: 
E/MyActivity: onDetachedFromWindow: 

因此如果是在 onCreate或者是onResume 里面获取View的 宽高,得到的都是0.

可以用View.post(Runnable)来延后 执行

本文地址:https://blog.csdn.net/GYBIN02/article/details/108129496