Can you fire an event when Android Dialog is dismissed?
Asked Answered
T

6

45

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

Takeover answered 1/6, 2011 at 15:29 Comment(0)
G
69

Use an OnDismissListener.

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

Gillies answered 1/6, 2011 at 15:35 Comment(3)
Warning, this approach is incompatible with DialogFragments as of API 11. See DialogFragment.onCreateDialog()Bergren
@Mahan did you find any alternative for this? ThanksSly
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.Stilbite
Y
11

Sure you can - check:

  public void onDismiss(DialogInterface dialogInterface)
  {
        //Fire event
  }
Yautia answered 1/6, 2011 at 15:35 Comment(0)
N
8

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.

Nosing answered 5/5, 2013 at 14:50 Comment(0)
B
6

Use setOnDismissListener method for the dialog.

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if (mIsSettingsDirty)
            refreshRecyclerView();
    }
});
Bestraddle answered 3/6, 2018 at 4:26 Comment(0)
C
2

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()
}
Chev answered 6/5, 2019 at 15:48 Comment(0)
A
0

If you want handle Dialog hiding, you can override 2 methods.

    @Override
    public void cancel() {
        super.cancel();
        callback();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        callback();
    }
Amick answered 17/1, 2020 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.