FragmentManagerImpl是如何commit进行添加

2020-06-11  本文已影响0人  小小亭长

在Activity的onCreated执行

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super(savedInstanceState);
        setContentView(R.layout.content_view);
        getSupportFragmentManager().beginTransaction().add(R.id.container, new Fragment()).commit();
        Looper.getMainLooper().dump(new LogPrinter(android.util.Log.DEBUG, "TAG"), "");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Looper.getMainLooper().dump(new LogPrinter(android.util.Log.DEBUG, "TAG"), "");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Looper.getMainLooper().dump(new LogPrinter(android.util.Log.DEBUG, "TAG"), "");
    }

根据日志如下:

06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG: Looper (main, tid 1) {125caec}
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 0: { when=-4h58m8s221ms callback=com.demo.debug.HandlerDebugManager$InnerActivityLifecycleCallbacks$1 target=android.os.Handler }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 1: { when=-20ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 2: { when=-13ms callback=android.view.View$UnsetPressedState target=android.view.ViewRootImpl$ViewRootHandler }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 3: { when=0 callback=androidx.fragment.app.FragmentManagerImpl$1 target=androidx.fragment.app.FragmentActivity$1 }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 4: { when=+654ms callback=androidx.lifecycle.ProcessLifecycleOwner$1 target=android.os.Handler }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 5: { when=+4m49s725ms what=2 target=org.chromium.base.SystemMessageHandler }
06-11 15:45:50.933 8193-8193/com.demo D/HandlerDebugManager_TAG:   (Total messages: 6, polling=false, quitting=false)
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG: onActivityStarted -- 
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG: Looper (main, tid 1) {125caec}
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 0: { when=-4h58m8s223ms callback=com.demo.debug.HandlerDebugManager$InnerActivityLifecycleCallbacks$1 target=android.os.Handler }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 1: { when=-22ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 2: { when=-15ms callback=android.view.View$UnsetPressedState target=android.view.ViewRootImpl$ViewRootHandler }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 3: { when=-2ms callback=androidx.fragment.app.FragmentManagerImpl$1 target=androidx.fragment.app.FragmentActivity$1 }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 4: { when=+652ms callback=androidx.lifecycle.ProcessLifecycleOwner$1 target=android.os.Handler }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 5: { when=+4m49s723ms what=2 target=org.chromium.base.SystemMessageHandler }
06-11 15:45:50.936 8193-8193/com.demo D/HandlerDebugManager_TAG:   (Total messages: 6, polling=false, quitting=false)
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG: onActivityResumed -- 
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG: Looper (main, tid 1) {125caec}
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 0: { when=-4h58m8s275ms callback=com.demo.debug.HandlerDebugManager$InnerActivityLifecycleCallbacks$1 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 1: { when=-74ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 2: { when=-67ms callback=android.view.View$UnsetPressedState target=android.view.ViewRootImpl$ViewRootHandler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 3: { when=-49ms callback=com.alibaba.wireless.security.open.edgecomputing.ECMiscInfo$1 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 4: { when=-39ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 5: { when=-36ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 6: { when=-34ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 7: { when=-34ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 8: { when=-33ms callback=androidx.core.content.res.ResourcesCompat$FontCallback$2 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 9: { when=-1ms callback=android.app.LoadedApk$ReceiverDispatcher$Args target=android.app.ActivityThread$H }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 10: { when=+600ms callback=androidx.lifecycle.ProcessLifecycleOwner$1 target=android.os.Handler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   Message 11: { when=+4m49s671ms what=2 target=org.chromium.base.SystemMessageHandler }
06-11 15:45:50.987 8193-8193/com.demo D/HandlerDebugManager_TAG:   (Total messages: 12, polling=false, quitting=false)

日志中出现了一条message3

Message 3: { when=0 callback=androidx.fragment.app.FragmentManagerImpl$1 target=androidx.fragment.app.FragmentActivity$1 }

可以推测出activity在新加fragment时加入了handler.post 来完成fragment的添加,但是为何在onResume中该message消失了,而排在该message之前的message任然存在呢?

解答

FragmentActivity源码如下

@Override
    protected void onStart() {
        super.onStart();

        mStopped = false;

        if (!mCreated) {
            mCreated = true;
            mFragments.dispatchActivityCreated();
        }

        mFragments.noteStateNotSaved();
        mFragments.execPendingActions();

        // NOTE: HC onStart goes here.

        mFragments.dispatchStart();  // -->dispatchStart()?
    }

FragmentController源码

public void dispatchStart() {
        mHost.mFragmentManager.dispatchStart(); // -->dispatchStart()?
    }

FragmentManager源码

public void dispatchStart() {
        mStateSaved = false;
        mStopped = false;
        dispatchStateChange(Fragment.STARTED);  // -->dispatchStateChange()?
    }

private void dispatchStateChange(int nextState) {
        try {
            mExecutingActions = true;
            moveToState(nextState, false);
        } finally {
            mExecutingActions = false;
        }
        execPendingActions();  // -->execPendingActions()?
    }

public boolean execPendingActions() {
        ensureExecReady(true); 

        boolean didSomething = false;
        while (generateOpsForPendingActions(mTmpRecords, mTmpIsPop)) {  // -->generateOpsForPendingActions()?
            mExecutingActions = true;
            try {
                removeRedundantOperationsAndExecute(mTmpRecords, mTmpIsPop);
            } finally {
                cleanupExec();
            }
            didSomething = true;
        }

        doPendingDeferredStart();
        burpActive();

        return didSomething;
    }

private boolean generateOpsForPendingActions(ArrayList<BackStackRecord> records,
            ArrayList<Boolean> isPop) {
        boolean didSomething = false;
        synchronized (this) {
            if (mPendingActions == null || mPendingActions.size() == 0) {
                return false;
            }

            final int numActions = mPendingActions.size();
            for (int i = 0; i < numActions; i++) {
                didSomething |= mPendingActions.get(i).generateOps(records, isPop);
            }
            mPendingActions.clear();
            mHost.getHandler().removeCallbacks(mExecCommit);  // -- removeCallbacks ?
        }
        return didSomething;
    }

最终定位在generateOpsForPendingActions()方法中将该Message移除出messagequeue,同时执行了Fragment的初始化

上一篇下一篇

猜你喜欢

热点阅读