I launch my dialog fragment using
FragmentTransaction ft = getFragmentManager().beginTransaction(); MyDialogFragment dialog = new MyDialogFragment() dialog.show(ft, "dialog");
then to get a handle on it I do
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
but once I get prev
, how do I check if it is showing?
Back Story
My problem is that my looping code keeps launching the dialog again and again. But if the dialog is already showing, I don't want it to launch again. This back story is just for context. The answer I seek is not: "move it out of the loop."
that should mean its in the foreground displaying if im not mistaken. If you call DialogFragment's creation several times in one moment, dialogFragment = getSupportFragmentManager(). findFragmentByTag("dialog"); will return null, and all dialogs will be shown.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
To create a DialogFragment , first create a class that extends DialogFragment , and override onCreateDialog() , as shown in the following example. Similar to how onCreateView() should create a root View in an ordinary fragment, onCreateDialog() should create a Dialog to display as part of the DialogFragment .
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.
if (dialogFragment != null && dialogFragment.getDialog() != null && dialogFragment.getDialog().isShowing() && !dialogFragment.isRemoving()) { //dialog is showing so do something } else { //dialog is not showing }
UPDATE: team can you also try the below call, i think it will work logically:
dialogFragment.isResumed
that should mean its in the foreground displaying if im not mistaken.
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