java.lang.RuntimeException: Unable to destroy activity {*}:java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

使用FragmentTransaction添加新的fragment,

ft.hide(from).add(viewId, to).commit();

偶尔会报以下错误,代码如下:

java.lang.RuntimeException: Unable to destroy activity {cn.healthcom.ihealthpad/cn.healthcom.ihealthpad.activity.MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4007)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4025)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4225)
    at android.app.ActivityThread.access$900(ActivityThread.java:182)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1499)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5682)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
    Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1842)
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1860)
    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:650)
    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:609)
    at cn.healthcom.ihealthpad.activity.MainActivity.hideAndShowFragment(MainActivity.java:958)
    at cn.healthcom.ihealthpad.activity.MainActivity$4.run(MainActivity.java:572)
    at android.app.Activity.runOnUiThread(Activity.java:5453)
    at cn.healthcom.ihealthpad.activity.MainActivity.switchFragment(MainActivity.java:562)
    at cn.healthcom.ihealthpad.activity.MainActivity$5.run(MainActivity.java:800)
    at android.app.Activity.runOnUiThread(Activity.java:5453)
    at cn.healthcom.ihealthpad.activity.MainActivity.onFocusChange(MainActivity.java:776)
    at android.view.View.onFocusChanged(View.java:5290)
    at android.view.View.handleFocusGainInternal(View.java:5035)
    at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:665)
    at android.view.View.requestFocusNoSearch(View.java:7770)
    at android.view.View.requestFocus(View.java:7749)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2726)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2727)
    at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2771)
    at android.view.ViewGroup.requestFocus(ViewGroup.java:2730)
    at android.view.View.requestFocus(View.java:7716)
    at android.view.View.requestFocus(View.java:7695)
    at android.view.View.rootViewRequestFocus(View.java:5182)
    at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4372)
    at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4327)
    at android.view.ViewGroup.removeView(ViewGroup.java:4258)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1411)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)

大概意思不能在onSaveInstanceState之后不能再用commit()来添加新的fragment,官方有解决的方案连接:http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()

把commit换成commitAllowingStateLoss 即可实现效果一样

ft.hide(from).add(viewId, to).commitAllowingStateLoss();