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:
Activity
- event is received so MyDialogFragment
is shown.Activity
once more time - MyDialogFragment
is shown again.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.
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");
}
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.
(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...
}
Just change the below line
MyDialogFragment.showMyDialog(name, this, this);
to
MyDialogFragment.showMyDialog(name, **"Activity class name.this"**, this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With