为什么处理程序在片段恢复后处理消息?

时间:2015-02-12 05:54:32

标签: android android-fragments android-ui android-fragmentactivity android-handler

我正在使用Handler替换Fragment上的ViewGroup 它似乎工作正常,但处理程序在片段恢复后处理其他消息 我怎么解释这种情况呢?

这是代码:

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Log.d(LOGTAG, "handleMessage " + msg.what);
        switch (msg.what) {
        case 0:
            Log.d(LOGTAG, "case 0 : Refresh page.");
            int type = msg.arg1;
            Fragment tempPage;
            switch(type) {
                default:
                case 0:
                    tempPage = new PageA();
                    break;
                case 1:
                    tempPage = new PageB();
                    break;
                case 2:
                    tempPage = new PageC();
                    break;
            }

            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.page_container, tempPage);
            fragmentTransaction.commitAllowingStateLoss();

            Log.d(LOGTAG, "sendEmptyMessageDelayed. 1");
            mHandler.sendEmptyMessageDelayed(2, 0);
            Log.d(LOGTAG, "sendEmptyMessageDelayed. 2");
            break;

        case 2:
            // something...

        }
    }
}

这里是logcat:

02-12 14:14:13.520: D/MainActivity(2062): handleMessage 0
02-12 14:14:13.520: D/MainActivity(2062): case 0 : Refresh page.
02-12 14:14:13.570: D/MainActivity(2062): sendEmptyMessageDelayed. 1
02-12 14:14:13.570: D/MainActivity(2062): sendEmptyMessageDelayed. 2
02-12 14:14:13.590: D/libEGL(2062): loaded /system/lib/egl/libEGL_VIVANTE.so
02-12 14:14:13.600: D/libEGL(2062): loaded /system/lib/egl/libGLESv1_CM_VIVANTE.so
02-12 14:14:13.660: D/libEGL(2062): loaded /system/lib/egl/libGLESv2_VIVANTE.so
02-12 14:14:13.730: E/BufferQueue(791): [jkpark.main/jkpark.main.MainActivity] dequeueBuffer: cant dequeue multiple buffers without setting the buffer count
02-12 14:14:13.730: D/Page.PageA(2062): onCreateView
02-12 14:14:13.860: D/dalvikvm(2062): GC_FOR_ALLOC freed 936K, 12% free 7670K/8636K, paused 12ms, total 13ms
02-12 14:14:14.050: D/Page.PageA(2062): onActivityCreated
02-12 14:14:14.050: D/Page.PageA(2062): onStart
02-12 14:14:14.050: D/Page.PageA(2062): onResume
02-12 14:14:14.050: D/MainActivity(2062): handleMessage 2

如您所见,处理案例0在14:14:13.570完成,但在片段恢复后打印下一个处理消息。
为什么?请解释一下。

0 个答案:

没有答案