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; } });
Sam Aug 27 '12 at 10:50 2012-08-27 22:50
source share