Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismissing DialogFragment programmatically

Tags:

android

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...

like image 386
Alex Avatar asked Nov 28 '11 17:11

Alex


People also ask

How do you dismiss a 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.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do I know if DialogFragment is showing?

This is gonna be disappointing to you "great" finding but just call progressDialog. showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


2 Answers

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.

like image 155
Zsombor Erdődy-Nagy Avatar answered Sep 27 '22 23:09

Zsombor Erdődy-Nagy


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();     } } 
like image 30
ol_v_er Avatar answered Sep 27 '22 22:09

ol_v_er