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

原文地址为: 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);
}
}
好了,特此纪念一下,再犯此错误,





转载请注明本文地址: DialogFragment使用中show()方法遇到的IllegalStateException
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值