Can you fire an event when Android Dialog is dismissed?

45,190

Solution 1

Use an OnDismissListener.

There is a setOnDismissListener(...) method in the class Dialog

Solution 2

Sure you can - check:

  public void onDismiss(DialogInterface dialogInterface)
  {
        //Fire event
  }

Solution 3

Whenever a dialog is closed either by clicking PositiveButton, NegativeButton, NeturalButton or by clicking outside of the dialog, "onDismiss" is always gets called automatically so do your stuff inside the onDismiss() method e.g.,

@Override
public void onDismiss(DialogInterface dialogInterface) {
    ...
}

You don't even need to call dismiss() method.

Solution 4

Use setOnDismissListener method for the dialog.

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mIsSettingsDirty)
            refreshRecyclerView();
    }
});

Solution 5

If you are within custom dialog class - override dismiss(). I recommend inserting logic BEFORE super.dismiss(). Kotlin example:

override fun dismiss() {
    Utils.hideKeyboard(mContext, window)
    super.dismiss()
}
Share:
45,190

Related videos on Youtube

ingh.am
Author by

ingh.am

Software Developer working in the East Riding of Yorkshire (UK). Computer Science graduate from The University of Hull with first class honours. SOreadytohelp

Updated on July 01, 2020

Comments

  • ingh.am
    ingh.am almost 4 years

    Say I have a created a dialog in my Android app like so:

    private static ProgressDialog dialog;
    dialog = ProgressDialog.show(MainActivity.this, "", "Downloading Files. Please wait...", true);
    

    Now, is it possible to fire an event when the following is called?

    dialog.dismiss();
    

    The reason I want to do this and not just call my method after dialog.dismiss(); is because the Dialog dismiss is called within a static class and the next thing I want to do is load a new Activity (which cannot be done using Intents within a static class).

  • RonR
    RonR about 10 years
    Warning, this approach is incompatible with DialogFragments as of API 11. See DialogFragment.onCreateDialog()
  • sandeepmaaram
    sandeepmaaram almost 7 years
    @Mahan did you find any alternative for this? Thanks
  • Yoav Feuerstein
    Yoav Feuerstein over 6 years
    Note that this doesn't work if the user clicks on Home button - in such case, the solution I found was to listen to when the activity which created the dialog has been dismissed.