Prevent Android activity dialog from closing on outside touch Prevent Android activity dialog from closing on outside touch android android

Prevent Android activity dialog from closing on outside touch


To prevent dialog box from getting dismissed on back key pressed use this

dialog.setCancelable(false);

And to prevent dialog box from getting dismissed on outside touch use this

 dialog.setCanceledOnTouchOutside(false);


What you actually have is an Activity (even if it looks like a Dialog), therefore you should call setFinishOnTouchOutside(false) from your activity if you want to keep it open when the background activity is clicked.

EDIT: This only works with android API level 11 or greater


What worked for me was to create DialogFragment an set it to not be cancelable:

dialog.setCancelable(false);