When I'm changing the orientation of my app with a visible DialogFragment
opened from my Activity
, the dialog will be recreated. How can I forcefully dismiss it?
For now I tried to store it as a member in my activity and dismiss it in onCreate but at this point it seems to be null;
To disable recreations of DialogFragments of a specific type, you can override onCreate
in your derived DialogFragment
class and dismiss the dialog in case it is being recreated:
public class MyDialogFragment extends DialogFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
dismiss();
}
}
...
As @Luksprog mentioned in the comments, a solution could be:
Give a tag to your DialogFragment at creation:
FragmentManager fragmentManager = getSupportFragmentManager();
MyDialogFragment.newInstance(...).show(fragmentManager, "myTag");
Search for it and dismiss it in onCreate
MyDialogFragment dialog = ((MyDialogFragment)getSupportFragmentManager().findFragmentByTag("myTag"));
if (dialog != null) {
dialog.dismiss();
}
I think it would be more efficient to disable the recreation of the DialogFragment altogether but I don't know if that's possible.
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