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:
- Enter
Activity- event is received soMyDialogFragmentis shown. - Dismiss dialog by negative button.
- Enter
Activityonce more time -MyDialogFragmentis shown again. - Type some code to the EditText.
- At this moment validation take some time. Connecting with server etc. And the negative result is returned. So it is need to show
MyDialogFragmentonce 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.