Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:31:45+00:00 2026-06-10T11:31:45+00:00

I am trying to prevent an AlertDialog box from closing when pressing the back

  • 0

I am trying to prevent an AlertDialog box from closing when pressing the back button in Android. I have followed both of the popular methods in this thread, and with System.out.println I can see that in both cases the code executes. However, the back button still closes the dialog box.

What could I be doing wrong? Ultimately I’m trying to prevent the back button closing a dialog box – it is a disclaimer that is displayed the first time the app is run and I don’t want the user to have any option but to press the “Accept” button in order for the app to continue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:31:47+00:00Added an answer on June 10, 2026 at 11:31 am

    Simply use the setCancelable() feature:

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

    This prevents the back button from closing the dialog, but leaves the “negative” button intact if you chose to use it.


    While any user that does not want to accept your terms of service can push the home button, in light of Squonk’s comment, here two more ways to prevent them from “backing out” of the user agreement. One is a simple “Refuse” button and the other overrides the back button in the dialog:

    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;
               }
           });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to prevent certain keys from being entered into an input box, but
I am trying to prevent VS from breaking on JS errors. I have the
I'm trying to prevent my button from flashing blue after clicking, anyone know what
Trying to prevent the application from shutting down when clicked the X button, it
I am trying to prevent users from double clicking on the submit button for
I am trying to prevent an android application from requesting lots of pemissions and
I'm trying to prevent a user from disabling style.(I HAVE A GOOD REASON TO
I'm trying to prevent an associated record from being deleted if it has associated
I'm trying to prevent user from inserting * in a textbox. This is what
I'm trying to prevent A HREF from actually opening the link, but to execute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.