setOnCancelListener and setOnDismissListener is not called for AlertDialog for back button pressed or touch outside

The problem happens when you are using DialogFragment to display Dialog

According to http://developer.android.com/reference/android/app/DialogFragment.html, the solution is to override onCancel in DialogFragment

Please take note from http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog(android.os.Bundle) too

Note: DialogFragment own the Dialog.setOnCancelListener and
Dialog.setOnDismissListener callbacks. You must not set them yourself.
To find out about these events, override onCancel(DialogInterface) and
onDismiss(DialogInterface).

// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}

Leave a Comment