setOnCancelListener and setOnDismissListener is not called for AlertDialog for back button pressed or touch outside
AndroidAndroid Problem Overview
When
- Touch outside the dialog region
- Press on back button
I'm expecting onDismiss
(Or onCancel
) will be called. However, both of them are not called. May I know is there anything I'm missing? From https://stackoverflow.com/questions/12525304/alertdialog-setondismisslistener-not-working, I thought onCancel
will be called when I press back button. But it doesn't work for me. May I know is there anything I had missed out?
public class RateAppDialogFragment extends SherlockDialogFragment {
public static RateAppDialogFragment newInstance() {
RateAppDialogFragment rateAppDialogFragment = new RateAppDialogFragment();
return rateAppDialogFragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.rate_app_dialog_fragment, null);
Utils.setCustomTypeFace(view, Utils.ROBOTO_LIGHT_TYPE_FACE);
final AlertDialog dialog = new AlertDialog.Builder(this.getSherlockActivity())
.setTitle("Love JStock?")
.setView(view)
// Add action buttons
.setPositiveButton("Rate 5 stars \u2605", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Rate");
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("No");
}
})
.setNeutralButton("Later", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Utils.showShortToast("Later");
}
})
.create();
dialog.setCanceledOnTouchOutside(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Utils.showShortToast("Back button pressed?");
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
Utils.showShortToast("Back button pressed?");
}
});
return dialog;
}
}
FragmentManager fm = fragmentActivity.getSupportFragmentManager();
if (fm.findFragmentByTag(RATE_APP_DIALOG_FRAGMENT) != null) {
return;
}
RateAppDialogFragment rateAppDialogFragment = RateAppDialogFragment.newInstance();
rateAppDialogFragment.show(fm, RATE_APP_DIALOG_FRAGMENT);
Android Solutions
Solution 1 - Android
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) {
}
Solution 2 - Android
If you are using AlertDialog
, see
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// dialog dismiss without button press
}
});
and dialog.setCanceledOnTouchOutside(true)
(thanks @LeosLiterak)
Solution 3 - Android
You have not set backPressed
key event
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
}
return true;
}
});
Solution 4 - Android
you should read this topic: https://stackoverflow.com/questions/8384067/how-to-dismiss-the-dialog-with-click-on-outside-of-the-dialog
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
Solution 5 - Android
in your DialogFragment, override
@Override
public void onStop() {
super.onStop();
if (mListener != null) {
// do something here
}
}