美文网首页
android Fragment的commit与commitAl

android Fragment的commit与commitAl

作者: 木木禾木 | 来源:发表于2020-04-24 16:03 被阅读0次
使用:

通常使用FragmentTransaction来add、replace、remove需要操作的fragment,然后通过commit或commitAllowingStateLoss应用这些变化。

关于Fragment的commit与commitAllowingStateLoss,有什么差别呢?
比如:

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.m_container, new RecyclerListFragment());
        //fragmentTransaction.commit();
        fragmentTransaction.commitAllowingStateLoss();
结论:
  1. commit() : 需要状态保持。即只能使用在在activity的状态存储之前,即在onSaveInstanceState(Bundle outState)之前调用,否则会抛异常。

  2. commitAllowingStateLoss() : 允许状态丢失。

源码中的方法说明
分析:

commit() 和 commitAllowingStateLoss() 的具体实现是在BackStackRecord这个类里,
如下:

@Override
    public int commit() {
        return commitInternal(false);
    }

    @Override
    public int commitAllowingStateLoss() {
        return commitInternal(true);
    }

    int commitInternal(boolean allowStateLoss) {
        if (mCommitted) throw new IllegalStateException("commit already called");
        if (FragmentManagerImpl.DEBUG) {
            Log.v(TAG, "Commit: " + this);
            LogWriter logw = new LogWriter(TAG);
            PrintWriter pw = new PrintWriter(logw);
            dump("  ", null, pw, null);
            pw.close();
        }
        mCommitted = true;
        if (mAddToBackStack) {
            mIndex = mManager.allocBackStackIndex(this);
        } else {
            mIndex = -1;
        }
        mManager.enqueueAction(this, allowStateLoss);
        return mIndex;
    }

mManager.enqueueAction(this, allowStateLoss)则在FragmentManager中,在此进行了状态检查checkStateLoss() ,如下:

    /**
     * Adds an action to the queue of pending actions.
     *
     * @param action the action to add
     * @param allowStateLoss whether to allow loss of state information
     * @throws IllegalStateException if the activity has been destroyed
     */
    public void enqueueAction(OpGenerator action, boolean allowStateLoss) {
        if (!allowStateLoss) {
            checkStateLoss();
        }
        synchronized (this) {
            if (mDestroyed || mHost == null) {
                throw new IllegalStateException("Activity has been destroyed");
            }
            if (mPendingActions == null) {
                mPendingActions = new ArrayList<>();
            }
            mPendingActions.add(action);
            scheduleCommit();
        }
    }

    private void checkStateLoss() {
        if (mStateSaved) {
            throw new IllegalStateException(
                    "Can not perform this action after onSaveInstanceState");
        }
        if (mNoTransactionsBecause != null) {
            throw new IllegalStateException(
                    "Can not perform this action inside of " + mNoTransactionsBecause);
        }
    }
扩展 :

DialogFragment的dismiss()dismissAllowingStateLoss()也是同理,最终也是通过FragmentTransaction的 commit 与 commitAllowingStateLoss 来操作。

DialogFragment

相关文章

网友评论

      本文标题:android Fragment的commit与commitAl

      本文链接:https://www.haomeiwen.com/subject/zrufwhtx.html