41

How does one programatically dismiss a DialogFragment? I am currently creating dialogs by:

void showDialogWithId(int id){

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);

    if (id == SEARCHING_DIALOG){

        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance(SEARCHING_DIALOG,"TEST");
        newFragment.show(ft, "dialog");
    }

    if (id == CONNECTING_DIALOG){

        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance(CONNECTING_DIALOG,"TEST");
        newFragment.show(ft, "dialog");
    }

    if (id == CONNECTIVITY_DIALOG){


        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance(CONNECTIVITY_DIALOG);
        newFragment.show(ft, "dialog");
    }

}

And I expect to dismiss them by:

public void dismissDialog(){

    getFragmentManager().popBackStack();

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        ft.remove(prev);
    }
    ft.addToBackStack(null);


}

However, the dialogs are not being dismissed...

ehehhh
  • 1,066
  • 3
  • 16
  • 27
Alex
  • 8,801
  • 5
  • 27
  • 31

5 Answers5

69

Try using

getDialog().dismiss();

Inside the DialogFragments. So for example you could find the DialogFragment by its tag, like you do so, and then call some method on it that calls this code. I'm usually don't initiate a dismiss of a DialogFragment from the Activity, my dialog buttons do that for me. But I think that this should also work. I'm not sure how this would affect the fragment backstack tho.

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101
  • 22
    Thanks - calling dismiss directly seems to work: DialogFragment dialogFragment = (DialogFragment)getSupportFragmentManager().findFragmentByTag("dialog"); if (dialogFragment != null) { dialogFragment.dismiss(); } – Alex Nov 29 '11 at 11:12
  • I don't really understand why I have to access the fragment from the manager this way, what prevents a value from being returned when we call getDialog directly? – Daniel Wilson Apr 09 '14 at 11:11
  • 1
    This is not the recommended way to remove dialog fragment. – uDevel May 12 '14 at 16:00
  • 1
    @uDevel and what is the recommended way according to you ? – Christ Jul 09 '14 at 11:02
  • The answer from ol_v_er is a good start. From my experience, it's better to use transaction, remove, commit, and executePendingTransactions to avoid dialog keep coming back on orientation change. – uDevel Jul 09 '14 at 16:51
  • ```getDialog().dismiss()``` and just ```dismiss()``` both causes activity to finish. Please see: http://stackoverflow.com/questions/40351042/dismiss-dialog-causes-activity-finish – JCarlosR Apr 07 '17 at 06:41
24

To aggregate the previous response and the associated comments, to remove the Dialog you have to do:

public void dismissDialog(){
    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
    if (prev != null) {
        DialogFragment df = (DialogFragment) prev;
        df.dismiss();
    }
}
ol_v_er
  • 27,094
  • 6
  • 48
  • 61
1

MyProgressDialog is a class which extends DialogFragment

// How to show

  MyProgressDialog   pd = new MyProgressDialog();
                    Bundle b = new Bundle();
                    b.putString(ProgressDialog.PROGRESS_TITLE, "Loading data");
                    b.putString(ProgressDialog.PROGRESS_MESSAGE, "Getting your data from server");
                    b.putBoolean(ProgressDialog.PROGRESS_IS_CALLBACK_NEEDED, false);
                    pd.setArguments(b);
                    pd.show(getActivity().getSupportFragmentManager(), ProgressDialog.class.getSimpleName());

// How to dismiss

     if (pd != null && pd.isAdded()) {
                        pd.dismiss();
                        pd =  null;
                    }
DeltaCap019
  • 6,532
  • 3
  • 48
  • 70
0

In my use case, I do the following:

In an Activity (or any other place) from you are showing dialog define a member variable:

private DialogFragment mMyDialog;

Then assign mMyDialog before showing it:

mMyDialog = MyDialog.newInstance(...whatever vars...); // MyDialog is extended from DialogFragment
mMyDialog.show();

And finally in the place where you need dismiss the dialog say:

if (mMyDialog != null) {
    mMyDialog.dismiss();
    mMyDialog = null;
}
Andrew
  • 36,676
  • 11
  • 141
  • 113
0

maybe it's will help anybody by for me , super.dismiss(); do the work.

Guy
  • 133
  • 2
  • 11