DialogFragment使用中show()方法遇到的IllegalStateException

  最近在首页做了一个弹窗,用dialogFragment 实现的,线上报了一个crash:

ava.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
	at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
特此记录一下。

此问题的原因是:mainActivity 调用了onSaveInstanceState()以后有触发了dialog的显示,dialog.show()方法里边用的是commit()而不是commitAllowingStateLoss()

追踪一下代码:

DialogFragment中:

public void show(FragmentManager manager, String tag) {
        mDismissed = false;
        mShownByMe = true;
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commit();
    }

从调用下手:

dialog.show(getActivity().getSupportFragmentManager(), "");

可以追踪下去,第一个参数manager是:
final FragmentManagerImpl mFragmentManager = new FragmentManagerImpl();
FragmentTransaction ft = manager.beginTransaction();
 @Override
    public FragmentTransaction beginTransaction() {
        return new BackStackRecord(this);
    }

BackStackRecord中:

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

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

可以看到这俩函数的区别就是commitInternal()方法中参数一个为true,一个为false,看一下这个函数:

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);
        }
        mCommitted = true;
        if (mAddToBackStack) {
            mIndex = mManager.allocBackStackIndex(this);
        } else {
            mIndex = -1;
        }
        mManager.enqueueAction(this, allowStateLoss);
        return mIndex;
    }
再追踪到enqueueAction(this,allowStateLoss):

public void enqueueAction(Runnable 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<Runnable>();
            }
            mPendingActions.add(action);
            if (mPendingActions.size() == 1) {
                mHost.getHandler().removeCallbacks(mExecCommit);
                mHost.getHandler().post(mExecCommit);
            }
        }
    }
可以看到函数第一行根据allowStateLoss做了一个判断,看一下checkStateLoss():

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);
        }
    }

怎么样,终于把这个异常信息给抓出来了吧,特么的,

知道了bug原因那我们来说一下解决吧,有人说用反射的方法拿到commitAllowingStateLoss()方法,调用他,但是这也太麻烦了,所简单点的办法就是:

重写show方法catch住异常即可,做好容错处理:

 /**
     * 为了解决:mainActivity调用onSaveInstanceState以后又调用了show方法,
     * 出现的Can not perform this action after onSaveInstanceState
     * 这个异常(不应该用commit ,而是用commitAllowingStateLoss)
     * 得罪了,不会反射 ,先把你catch住吧.乖
     * @param manager
     * @param tag
     */
    @Override
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (IllegalStateException ignore) {
            //  容错处理,不做操作
        }
    }

另外别忘记dissmiss也要做处理:

/**
     * 注意,不要用super.dismiss(),bug 同上show()
     * super.onDismiss就没问题
     */
    public void dismissDialog() {
        if ( getActivity() != null && !getActivity().isFinishing()) {
            super.dismissAllowingStateLoss();
        }
    }
而这个方法是没问题的:

@Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }
因为他调用的就是:

public void onDismiss(DialogInterface dialog) {
        if (!mViewDestroyed) {
            // Note: we need to use allowStateLoss, because the dialog
            // dispatches this asynchronously so we can receive the call
            // after the activity is paused.  Worst case, when the user comes
            // back to the activity they see the dialog again.
            dismissInternal(true);
        }
    }
好了,特此纪念一下,再犯此错误,




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章