从AppCompatActivity返回后显示DialogFragment时出现IllegalStateException

时间:2016-05-03 16:57:57

标签: android android-fragments android-fragmentactivity android-dialogfragment illegalstateexception

我从自定义的DialogFragment启动FragmentActivity,在我回到原始Activity并尝试再次打开对话框后,我得到:

  

IllegalStateException:无法在onSaveInstanceState

之后执行此操作

我不明白为什么会发生这种情况,如果我从原始活动中解除对话,而不是我可以多次再次显示它,但如果我从DialogFragment开始一个新的Activity,那么我就无法显示任何对话,因为我得到了例外。

这是我的代码,这个方法在我的DialogFragment中:

 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){
     mSearchLocationType = position;
     switch(position){
         case PSL_CURRENT_POSITION: // I can show the dialog again after this.
             break;
         case PSL_MAP_POINT:
             Intent selectMapPoint = new Intent(parentActivity, SelectMapPointActivity.class);     
             selectMapPoint.putExtra(SelectMapPointFragment.EXTRA_SELECTED_POS, mSearchLocation);
             parentActivity.startActivityForResult(selectMapPoint, REQ_MAP_POINT); 
             // After returning this Activity and trying to show ANY DialogFragment the app crashes.
             break;
         case PSL_ADDRESS:
             Intent selectAddress = new Intent(parentActivity, SelectAddressActivity.class);
             parentActivity.startActivity(selectAddress); // Also from this Activity, makes no difference.
             break;
      }
      dismiss();
 }    

我正在显示如下对话框:

if(!selectPoiAroundDialog.isVisible())
    selectPoiAroundDialog.show(parentActivity.getSupportFragmentManager(), "mSelectPoiCategoryDialog");

我已经尝试过了:

  • 覆盖DialogFragment的show()方法并使用此代码显示对话框:

      

    FragmentTransaction ft = manager.beginTransaction();
      ft.add(this,tag);
      ft.commitAllowingStateLoss();

  • 调试应用程序并检查是否所有生命周期方法都被调用,但对我来说这似乎是正常的,每次调用DialogFragment的onDestroyView()方法。

  • 覆盖DialogFragment的show()和onDismiss()方法,并跟踪它是否显示。

这是一个企业应用程序,设计不好,所以我试图修改的类(我在显示对话框的地方)不是Activity或Fragment的子代。 'parentActivity'是由应用程序类的静态方法访问的,但我确信它具有正确的Activity。你认为这会导致问题吗?

修改 完整的堆栈跟踪是我使用commitAllowStateLoss():

的时候
 java.lang.IllegalStateException: Activity has been destroyed
 at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
 at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
 at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:621)
 at com.mycompany.dialog.GTDialog.show(GTDialog.java:174)
 at com.mycompany.fragment.content.SearchOfflinePOIContent$2.onClick(SearchOfflinePOIContent.java:250)
 at android.view.View.performClick(View.java:5201)
 at android.view.View$PerformClick.run(View.java:21209)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:148)
 at android.app.ActivityThread.main(ActivityThread.java:5525)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
  • 我还尝试覆盖onSaveInstanceState()方法,但没有帮助。

EDIT2: 原始的堆栈跟踪是:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1493)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1511)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
at com.mycompany.dialog.GTDialog.show(GTDialog.java:174)
at com.mycompany.fragment.content.SearchOfflinePOIContent$1.onClick(SearchOfflinePOIContent.java:197)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

如果我覆盖show()方法并尝试使用commitAllowingStateLoss()提交片段,那么我会得到第一个异常。

1 个答案:

答案 0 :(得分:1)

我弄明白了这个问题。当应用程序退出活动时,它没有从parentActivity中删除它的引用,所以当我尝试再次显示该对话框时,我提供了已销毁活动的引用。

感谢您的时间!