我的应用中有一个包含 3 个片段的活动。当在第一个按钮上按下按钮时,会在 Activity 中调用一个方法,该方法会执行一些不相关的操作,然后调用finish()
.
这会在片段上触发onPause()
,片段会做更多不相关的事情,然后调用super.onPause()
.
onDestroyView()
然后,应用程序加载它出现的旧 Activity(逻辑上遵循视图堆栈),并在 Fragment 上调用时突然崩溃并出现以下异常:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to destroy activity {be.niteowl.niteowl.debug/be.niteowl.niteowl.views.activities.VenueActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:2811)
at android.app.ActivityThread.access$2100(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:972)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
at be.niteowl.niteowl.views.activities.TrackFragment.onDestroyView(TrackFragment.java:161)
at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1665)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:980)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1898)
at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:324)
at com.actionbarsherlock.app.SherlockFragmentActivity.onDestroy(SherlockFragmentActivity.java:88)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:2780)
... 11 more
MapFragment
尝试从主片段中删除 a 时似乎发生了错误。我知道您不应该在片段中包含片段,但是代码过去可以工作,无论如何也无法解决。
@Override
public void onDestroyView() {
SupportMapFragment f = (SupportMapFragment) activity.getSupportFragmentManager().findFragmentById(R.id.fragment_map);
if (f != null) {
activity.getSupportFragmentManager().beginTransaction().remove(f).commit();
}
super.onDestroyView();
}
我已经在 SO 上四处寻找修复(最明显的是出现异常 "IllegalStateException: Can not perform this action after onSaveInstanceState"),但它们都不适用于我当前的问题。