Android - 由于IllegalStateException导致无法在完成后销毁活动:在onSaveInstanceState之后无法执行此操作

时间:2013-07-17 13:28:07

标签: android android-fragments illegalstateexception ondestroy supportmapfragment

我的应用中有3个片段的活动。当按下第一个按钮时,会在Activity中调用一个方法来执行一些不相关的操作,然后调用finish()

这会在片段上触发onPause(),它会执行更多不相关的内容,然后调用super.onPause()

该应用程序然后加载它出现的旧活动(逻辑上跟随视图堆栈),并且在片段上调用onDestroyView()时突然崩溃并出现以下异常:

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时,似乎发生了错误。我知道你不应该在片段中有碎片,但是代码在过去是有效的,无论如何都无法绕过它。

@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上寻找解决方案(最值得注意的是getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState"),但它们都不适用于我当前的问题。

4 个答案:

答案 0 :(得分:17)

以下代码对我有用。它放在调用地图片段的片段中

@Override
public void onDestroy() {

    SupportMapFragment f = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapView);
    if (f.isResumed()){ 
        getFragmentManager().beginTransaction().remove(f).commit();
    }

    super.onDestroy();
}

答案 1 :(得分:8)

由于您的活动正在被销毁,您的片段将自动被销毁。因此,您无需删除它们。只需删除onDestroyView()方法。

  

我知道你不应该在片段中包含片段,但代码在过去是有效的,无论如何都无法绕过它。

首先,片段内的片段通过Android支持包的片段后端和片段的原生Android 4.2+实现得到支持。

其次,你没有在片段中做片段,至少不是基于这里显示的代码。出于某种原因,您的TrackFragment正在管理另一个顶级片段(您的SupportMapFragment),而不是将该工作委托给该活动。

答案 2 :(得分:0)

OnPostResume()回调中提交交易为我解决了这个问题。感谢关注博文http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html

@Override
protected void onPostResume() {
    super.onPostResume();
    // Commit your transactions here.
}

答案 3 :(得分:0)

当您的活动被销毁导致碎片被破坏时,无法处理.commit()。

删除getSupportFragmentManager()。beginTransaction()。remove()。commit()。

尝试在onBackPressed()中处理,我为我的应用程序做了