Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 799
woyaru Avatar asked Apr 25 '14 13:04

woyaru


4 Answers

In the method showMyDialog() first try :

MyDialogFragment fragment = activity.findFragmentByTag("myDialog");

if (fragment == null), then initialize :

MyDialogFragment fragment = new MyDialogFragment(listener, "Hello " + name);

else show it :

fragment.show(activity.getFragmentManager(), "myDialog");

UPDATE :

void showMyDialog() {
    MyDialogFragment fragment = activity.getFragmentManager().findFragmentByTag("myDialog");
    if (fragment == null) {
        fragment = new MyDialogFragment(listener, "Hello " + name);
    }
    fragment.show(activity.getFragmentManager(), "myDialog");
}
like image 90
Ankit Bansal Avatar answered Oct 27 '22 08:10

Ankit Bansal


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.

like image 27
woyaru Avatar answered Oct 27 '22 07:10

woyaru


(For API 17 and up)

In case anyone is still having this problem (or even related with a transaction's commit() method) I just added a check before it:

private showFragment() {
    if (isDestroyed()) {
        return;
    }

    // Else do whatever transaction...
}
like image 4
josemigallas Avatar answered Oct 27 '22 06:10

josemigallas


Just change the below line

MyDialogFragment.showMyDialog(name, this, this);

to

MyDialogFragment.showMyDialog(name, **"Activity class name.this"**, this);
like image 2
gandhi Avatar answered Oct 27 '22 06:10

gandhi