Prevent the back button from closing the dialog box

I am trying to prevent the AlertDialog window from closing when I click the back button in Android. I stuck with both popular methods in this thread , and with System.out.println I can see that in both cases the code is executing. However, the back button still closes the dialog box.

What can i do wrong? Ultimately, I try to prevent the "Back" button from closing the dialog box - this is an expression of failure that appears the first time the application is launched, and I do not want the user to have any option, but click the "Accept" button in the application went on.

+46
android back
Aug 27 '12 at 22:48
source share
5 answers

Just use setCancelable() :

 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setCancelable(false); 

This prevents the back button dialog box from closing, but leaves the negative button intact if you decide to use it.




While any user who does not want to accept your terms of service can click the home button, in the light of Squonkโ€™s comment, there are two more ways to prevent the userโ€™s termination. One of them is a simple "Refuse" button, and the other overrides the "Back" button in the dialog box:

 builder.setNegativeButton("Refuse", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }) .setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) finish(); return false; } }); 
+104
Aug 27 '12 at 10:50
source share

So that the Back button does not close the dialog box (regardless of Activity), just the following code:

 alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { // Prevent dialog close on back press button return keyCode == KeyEvent.KEYCODE_BACK; } }); 
+10
Jan 23 '15 at 13:29
source share

Use setCancelable(false)

 SampleDialog sampleDialog = SampleDialog.newInstance(); sampleDialog.setCancelable(false); sampleDialog.show(getSupportFragmentManager(), SampleDialog.class.getSimpleName()); 
+1
Feb 29 '16 at 17:35
source share

Only this worked for me:

 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(title); builder.setMessage(content); /** * Make it when the Back button is pressed, the dialog isn't dismissed. */ builder.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { Utilities.makeToast(getContext(), "There is no way back!"); return true; // Consumed } else { return false; // Not consumed } } }); Dialog dialog = builder.create(); /** * Make it so touching on the background activity doesn't close the dialog */ dialog.setCanceledOnTouchOutside(false); return dialog; 

As you can see, I also added the line dialog.setCanceledOnTouchOutside(false); , so clicking outside the dialog box does not close it.

0
Jul 16 '16 at 12:15
source share

When using DialogFragment you will need to call setCancelable() on the fragment, and not in the dialog box:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { dialog = new ProgressDialog(getActivity()); dialog.setIndeterminate(true); dialog.setMessage(...); setCancelable(false); return dialog; } 

Calling dialog.setCancelable() seems inefficient. It seems that DialogFragment does not pay attention to setting the dialog to cancel.

0
Aug 31 '17 at 8:47
source share



All Articles