真的必须要在主线程更新UI吗?
一般UI更新都会调用requestLayout 或者invalidate, 自定义view 也要如此 ???。
@android.view.RemotableViewMethod
public void setWidth(int pixels) {
mMaxWidth = mMinWidth = pixels;
mMaxWidthMode = mMinWidthMode = PIXELS;
requestLayout();
invalidate();
}
@Override
public void requestLayout() {//不会调用draw
if (!mHandlingLayoutInLayoutRequest) {
checkThread();
mLayoutRequested = true;
scheduleTraversals();// 1
}
}
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
“Only the original thread that created a view hierarchy can touch its views.”);
}
}
而在onCreate直接去更新UI,不走上述requestLayout 或者invalidate。其堆栈是这样的:
2020-08-09 09:39:48.374 3561-3561/com.yz.myapplication D/yyzz: android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) android.app.ActivityThread
2020-08-09 09:39:48.374 3561-3561/com.yz.myapplication D/yyzz: com.yz.myapplication.MainActivit
本文地址:https://blog.csdn.net/qq_42894864/article/details/107890763