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…
Try using
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.