IllegalStateException: Activity has been destroyed - when app is trying to show DialogFragment once more time

woyaru picture woyaru · Apr 25, 2014 · Viewed 8.6k times · Source

I have found a lot of similar questions on the stackoverflow but their solutions don't work in my case. They are connected with onSaveInstanceState method and the Support Library especially.

I have Activity which runs on one event this code:

MyDialogFragment.showMyDialog(name, this, this);

name parameter is the String object. Second parameter (this) is the just Activity class object and the third one (also this) is the simple interface. This my Activity implements this interface. showMyDialog() is of course static method. This is it body:

MyDialogFragment fragment = new MyDialogFragment(listener, "Hello " + name);
fragment.show(activity.getFragmentManager(), "myDialog");

This is working good at the first try. But at the second I am getting this exception:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapp, PID: 20759
java.lang.IllegalStateException: Activity has been destroyed
        at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1345)
        at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
        at android.app.BackStackRecord.commit(BackStackRecord.java:575)
        at android.app.DialogFragment.show(DialogFragment.java:230)
        at com.example.myapp.view.dialog.MyDialogFragment.showMyDialog(MyDialogFragment.java:41)
        at com.example.myapp.MyActivity.showMyDialog(MyActivity.java:208)
        at com.example.myapp.MyActivity.onEvent(MyActivity.java:232)
        at com.example.myapp.MyActivity.handleMessage(MyActivity.java:89)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

In general this is the steps to reproduce my exception:

  1. Enter Activity - event is received so MyDialogFragment is shown.
  2. Dismiss dialog by negative button.
  3. Enter Activity once more time - MyDialogFragment is shown again.
  4. Type some code to the EditText.
  5. At this moment validation take some time. Connecting with server etc. And the negative result is returned. So it is need to show MyDialogFragment once more. But at this moment I am getting exception.

However, when I skip the second point and start to typing wrong code for validation, MyDialogFragment will be shown without any issue. Strange behaviour.

I have tried with non-static method, setRetainInstance(true) and also commitAllowingStateLoss. But there was no difference.

Answer

woyaru picture woyaru · Jul 7, 2014

It is a bit odd - or maybe just a bug in this Android functionality. I have added block try catch to catch throwing exception in this way:

MyDialogFragment fragment = new MyDialogFragment(listener, "Hello " + name);
try {
    fragment.show(activity.getFragmentManager(), "myDialog");
} catch (Exception e) {
    e.printStackTrace();
}

And of course exception is still throwing (and catching at this moment) and what is interesting my dialog fragment is recreating in correct way and user can interact with it.