Private event DialogFragment

I need to process the end of DialogFragment (after calling .dismiss) - for example, I would show a toast inside an action that "contains" a fragment after being fired.

How to handle the event?

+6
source share
3 answers

Override onDismiss() in the DialogFragment dialog box or use setOnDismissListener() in the code block where you create the fragment.

+17
source

I had a similar problem, but I wanted to report another action about turning off the dialog (and not about the activity that created and displayed the dialog).

Although you can simply override the onDismiss() method in your dialog mode, as suggested by Austyn Mahoney, you cannot use setOnDismissListener() because DialogFragment simply does not provide such a method (according to: Android Developers Dialog Reference ).

But still there is another good way to inform any other activity about turning off the dialog , (I found it there: DialogFragment and onDismiss ), here it is:

First, you must do your action (the one that you want to convey information about turning off the dialog) to implement OnDismissListener :

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

Again, according to the Android Developers Dialog Interface Reference, DialogFragment already implements OnDismissListener using the onDismiss() method. This is why you should override it and call your onDismiss() method there, which you implemented in YourActivity:

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

You can give an event on a button, for example

 img_popup_timer_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "Cancel", Toast.LENGTH_SHORT).show(); **getDialog().cancel();** } }); 
0
source

All Articles