关闭

跟进View的常见周期回调以及View.post和Activity.runOnUiThread

标签: android
466人阅读 评论(0) 收藏 举报
分类:

activity_main.xml:

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

AndroidManifest.xml:

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

MainActivity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final View view = findViewById(R.id.text_view);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            System.err.println(Thread.currentThread() + " + Activity.runOnUiThread(Runnable)");
        }
    });
    if (view != null) {
        view.post(new Runnable() {
            @Override
            public void run() {
                System.err.println(Thread.currentThread() + " + View.post(Runnable)");
            }
        });
    }
}

自定义TextView覆写的”生命周期”回调方法:

onAttachedToWindow();
onDetachedFromWindow();
onWindowVisibilityChanged(int visibility);
onWindowFocusChanged(boolean hasWindowFocus);
onVisibilityChanged(View changedView, int visibility);
onFinishInflate();
onLayout(boolean changed, int left, int top, int right, int bottom);
onMeasure(int widthMeasureSpec, int heightMeasureSpec);
onDraw(Canvas canvas);
onSizeChanged(int w, int h, int oldw, int oldh);

下面是日志:
launch:

  1. after first onMeasure, getMeasuredWidth() and getMeasuredHeight() worked;
  2. onSizeChanged or after, getMeasuredWidth()/getMeasuredHeight()/getWidth()/getHeight() worked, until after onDetachedFromWindow;
08-30 12:33:07.680 9816-9816/com.test W/System.err: onFinishInflate
08-30 12:33:07.681 9816-9816/com.test W/System.err: Thread[main,5,main] + Activity.runOnUiThread(Runnable)
08-30 12:33:07.708 9816-9816/com.test W/System.err: onAttachedToWindow
08-30 12:33:07.708 9816-9816/com.test W/System.err: onWindowVisibilityChanged
08-30 12:33:07.708 9816-9816/com.test W/System.err: onVisibilityChanged
08-30 12:33:07.712 9816-9816/com.test W/System.err: onMeasure
08-30 12:33:07.771 9816-9816/com.test W/System.err: onMeasure
08-30 12:33:07.771 9816-9816/com.test W/System.err: onSizeChanged
08-30 12:33:07.771 9816-9816/com.test W/System.err: onLayout
08-30 12:33:07.802 9816-9816/com.test W/System.err: Thread[main,5,main] + View.post(Runnable)
08-30 12:33:07.802 9816-9816/com.test W/System.err: onWindowFocusChanged
08-30 12:33:07.804 9816-9816/com.test W/System.err: onMeasure
08-30 12:33:07.804 9816-9816/com.test W/System.err: onLayout
08-30 12:33:07.804 9816-9816/com.test W/System.err: onDraw

home:

08-30 12:35:38.628 9816-9816/com.test W/System.err: onWindowFocusChanged
08-30 12:35:38.747 9816-9816/com.test W/System.err: onWindowVisibilityChanged
08-30 12:35:38.989 9816-9816/com.test W/System.err: onVisibilityChanged

re-foreground:

08-30 12:35:59.959 9816-9816/com.test W/System.err: onVisibilityChanged
08-30 12:35:59.967 9816-9816/com.test W/System.err: onWindowVisibilityChanged
08-30 12:35:59.988 9816-9816/com.test W/System.err: onWindowFocusChanged
08-30 12:36:00.001 9816-9816/com.test W/System.err: onDraw

back:

08-30 12:36:25.576 9816-9816/com.test W/System.err: onWindowFocusChanged
08-30 12:36:25.668 9816-9816/com.test W/System.err: onWindowVisibilityChanged
08-30 12:36:25.678 9816-9816/com.test W/System.err: onDetachedFromWindow

re-launch or rotate-screen:

08-30 12:36:50.175 9816-9816/com.test W/System.err: onFinishInflate
08-30 12:36:50.175 9816-9816/com.test W/System.err: Thread[main,5,main] + Activity.runOnUiThread(Runnable)
08-30 12:36:50.182 9816-9816/com.test W/System.err: onAttachedToWindow
08-30 12:36:50.182 9816-9816/com.test W/System.err: onWindowVisibilityChanged
08-30 12:36:50.182 9816-9816/com.test W/System.err: onVisibilityChanged
08-30 12:36:50.183 9816-9816/com.test W/System.err: onMeasure
08-30 12:36:50.219 9816-9816/com.test W/System.err: onMeasure
08-30 12:36:50.220 9816-9816/com.test W/System.err: onSizeChanged
08-30 12:36:50.220 9816-9816/com.test W/System.err: onLayout
08-30 12:36:50.222 9816-9816/com.test W/System.err: Thread[main,5,main] + View.post(Runnable)
08-30 12:36:50.222 9816-9816/com.test W/System.err: onWindowFocusChanged
08-30 12:36:50.232 9816-9816/com.test W/System.err: onMeasure
08-30 12:36:50.232 9816-9816/com.test W/System.err: onLayout
08-30 12:36:50.232 9816-9816/com.test W/System.err: onDraw

lock-screen or show-status-panel:

08-30 12:38:12.882 9816-9816/com.test W/System.err: onWindowFocusChanged

unlock-screen or hide-status-panel:

08-30 12:38:12.882 9816-9816/com.test W/System.err: onWindowFocusChanged

关于View.post和Activity.runOnUiThread的札记

先看Activity的runOnUiThread的源码:

final Handler mHandler = new Handler();
public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else {
        action.run();
    }
}

可以看出, 如果当前是主线程则直接执行, 否则立即投递到主线程Handler执行;
再看View的post的源码:

public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }
    // Assume that post will succeed later
    ViewRootImpl.getRunQueue().post(action);
    return true;
}

如果mAttachInfo不为null, 则投递到它的Handler中执行, 否则加入到ViewRootImpl.getRunQueue()这个队列里.
那么什么时候, mAttachInfo不为null呢? 这里我直接给出答案: (请按照此流程浏览源码)

我们跳过一些路径, 从Activity的onResume()开始说, 它是在handleResumeActivity()方法里的performResumeActivity()里完成的.
在这之后, handleResumeActivity()里, 有调用WindowManagerImpl.addView()将PhoneWindow.mDecorView(DecorView类型, 继承自FrameLayout)添加到窗体中.
然后路径到了WindowManagerGlobal.addView()(一个 Activity对应一个WindowManagerImpl, 但WindowManagerGlobal进程唯一), 这里new ViewRootImpl(), 调用此对象的setView().
setView()里赋值ViewRootImpl的mAttachInfo, 然后首次调用requestLayout(), 此方法里调用scheduleTraversals(), 进而调用 performTraversals().
performTraversals()里调用顶层view的dispatchAttachedToWindow(), 因为顶层view是FrameLayout, 所以调用ViewGroup的dispatchAttachedToWindow(), 里面遍历调用所有子view的dispatchAttachedToWindow(), 并传入mAttachInfo(一直传递), 直到调用View类的dispatchAttachedToWindow().
此时, mAttachInfo不再为null, 而且各层的onAttachedToWindow()得到回调.
另外, onAttachedToWindow()真正执行顺序依次是 View.onAttachedToWindow(), ViewGroup.onAttachedToWindow(), DecorView.onAttachedToWindow()(当然表面的调用顺序是反过来的, 使用super执行).
所以你在Activity的onCreate()里调用view.post, 实际走的是:ViewRootImpl.getRunQueue().post(action); 看代码:

static final class RunQueue {
        private final ArrayList<HandlerAction> mActions = new ArrayList<HandlerAction>();

        void post(Runnable action) {
            postDelayed(action, 0);
        }

        void postDelayed(Runnable action, long delayMillis) {
            HandlerAction handlerAction = new HandlerAction();
            handlerAction.action = action;
            handlerAction.delay = delayMillis;

            synchronized (mActions) {
                mActions.add(handlerAction);
            }
        }

        void executeActions(Handler handler) {
            synchronized (mActions) {
                final ArrayList<HandlerAction> actions = mActions;
                final int count = actions.size();

                for (int i = 0; i < count; i++) {
                    final HandlerAction handlerAction = actions.get(i);
                    handler.postDelayed(handlerAction.action, handlerAction.delay);
                }

                actions.clear();
            }
        }
    }

也就是post只是加入到了数组中, 关键是什么时候调用executeActions(Handler)?
答案还是在ViewRootImpl的performTraversals()里调用, 而且还是使用mAttachInfo的Handler.
其实, 这个Handler是在ViewRootImpl的构造方法中传递给mAttachInfo(AttachInfo)的构造器的, 这个Handler在ViewRootImpl中使用主线程Looper构建, 属于ViewRootHandler实例.

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    文章分类
    最新评论