最近在首頁做了一個彈窗,用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);
}
}
好了,特此紀念一下,再犯此錯誤,