Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment and onDismiss

I am using a DialogFragment, which I am showing like this from an Activity:

DialogFragmentImage dialog = DialogFragmentImage.newInstance(createBitmap()); dialog.onDismiss(dialog);.onDismiss(this);           dialog.show(getFragmentManager(), "DialogFragmentImage"); 

I would like to check when the DialogFragment was dismissed (for example when the back button was pressed), but in my Activity. How can I do that? How can I "tell" my activity that the DialogFragment has been dismissed?

like image 441
deimos1988 Avatar asked May 21 '14 14:05

deimos1988


People also ask

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.

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.

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?

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.


2 Answers

Make your Activity implement OnDismissListener

public final class YourActivity extends Activity implements DialogInterface.OnDismissListener {      @Override     public void onDismiss(final DialogInterface dialog) {         //Fragment dialog had been dismissed     }  } 

DialogFragment already implements OnDismissListener, just override the method and call the Activity.

public final class DialogFragmentImage extends DialogFragment {      ///blah blah      @Override     public void onDismiss(final DialogInterface dialog) {         super.onDismiss(dialog);         final Activity activity = getActivity();         if (activity instanceof DialogInterface.OnDismissListener) {             ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);         }     }  } 

If you're starting the dialog from a fragment using the childFragment manager (API>=17), you can use getParentFragment to talk to the onDismissListener on the parent fragment.:

public final class DialogFragmentImage extends DialogFragment {      ///blah blah      @Override     public void onDismiss(final DialogInterface dialog) {         super.onDismiss(dialog);         Fragment parentFragment = getParentFragment();         if (parentFragment instanceof DialogInterface.OnDismissListener) {             ((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog);         }      }  } 
like image 124
Yaroslav Mytkalyk Avatar answered Sep 30 '22 13:09

Yaroslav Mytkalyk


Here is my answer. It's a bit late but it's maybe benefit someone passing by.

FragmentManager fm = getFragmentManager();  YourDialogFragment dialog = new YourDialogFragment(); dialog.show(fm,"MyDialog");  fm.executePendingTransactions(); dialog.getDialog().setOnDismissListener(new DialogInterface.OnDismissListener() {                     @Override                     public void onDismiss(DialogInterface dialogInterface) {                        //do whatever you want when dialog is dismissed                     }                 }); 

We need to call

fm.executePendingTransactions();  

To make sure that FragmentTransaction work has been performed. Otherwise NullPointerException can occur when calling setOnDismissListener().

Sorry if there is any mistake. Hope this help.

like image 44
Boonya Kitpitak Avatar answered Sep 30 '22 11:09

Boonya Kitpitak