AlertDialog's setCancelable(false) method not working

I had created an AlertDialog which is working fine. It is disappearing, if I press:
1) escape keyboard button or
2) back button using mouse
To make it stay focused even on above stated conditions, I had added '.setCancelable(false)' statement while building. But, I still see dialog disappearing. Where is the problem? Please help.

Code added:

return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(title)
.setCancelable(false)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)
.create();


Env: Android 4.0 on XP Professional.

74913 次浏览

Is this your complete code? then please change your code for setting setCancelable(false) like this

void showDialog() {
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string..alert_dialog_two_buttons_title);
newFragment.setCancelable(false);
newFragment.show(getFragmentManager(), "dialog");
}

Another working example:

Step 1

Create class:

public class DialogActivity extends android.app.DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.myMessage);
setCancelable(false);
return builder.create();
}
}

Step 2

Add method to your Activity:

private boolean showDialog() {
FragmentManager manager = getFragmentManager();
DialogActivity dialogActivity;
dialogActivity = new DialogActivity();
dialogActivity.show(manager, "DialogActivity");
return true;
}

Step 3

Call showDialog() when you need to show dialog

Your dialog is set to no-cancelable, but your host fragment is still cancelable. Set your fragment with setCancelable(false).

dialog.setCanceledOnTouchOutside(false);

setCanceledOnTouchOutside(boolean)

Sets whether this dialog is canceled when touched outside the window's bounds. If setting to true, the dialog is set to be cancelable if not already set.

The simplest way to implement "setCancelable" is to implement the same when calling the dialog in the activity; That way, not directly in the dialog class.

 Dialog myDialog = new Dialog();
myDialog.setCancelable( false );
myDialog.show( getSupportFragmentManager(),"dialog" );
return true;

In Kotlin for making dialog non-dismissible

dialog.isCancelable = false

Based on your AlertDialog type you can:

AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.MyAlertDialogLayout).setCancelable(false);

or

AlertDialog alertDialog = builder.create();


alertDialog.setCancelable(false);

In DialogFragment

Used:-

dialog.setCanceledOnTouchOutside(false)

        builder.setTitle("There's a problem")
.setMessage(error)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setNegativeButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.show();
});