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.
setCancelable(false) means that back key doesn't close the dialog. If for example you want the dialog to be closed only if the user click some dialog button then both setCanceledOnTouchOutside() and setCancelable() should be set to false.
setCanceledOnTouchOutside only prevents dismissing by clicking outside of the dialog. But you can still dismiss it with back button for instance. If you don't want your dialog to be cancelable at all use dialog.setCancelable(false)
Alert Dialog shows the Alert message and gives the answer in the form of yes or no. Alert Dialog displays the message to warn you and then according to your response the next step is processed. Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button.
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With