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 663227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:28:00+00:00 2026-05-13T23:28:00+00:00

I have an activity that contains several user editable items (an EditText field, RatingBar,

  • 0

I have an activity that contains several user editable items (an EditText field, RatingBar, etc). I’d like to prompt the user if the back/home button is pressed and changes have been made that have not yet been saved. After reading through the android documentation, it seems like this piece of code should go in the onPause method. I’ve tried putting an AlertDialog in the onPause however the dialog gets shown and then immediately tears down because nothing is there to block the pause from completing.

This is what I’ve come up with so far:

@Override
protected void onPause() {
    super.onPause();

    AlertDialog ad = new AlertDialog.Builder(this).setMessage(
            R.string.rating_exit_message).setTitle(
            R.string.rating_exit_title).setCancelable(false)
            .setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects OK, save changes to db
                        }
                    }).setNeutralButton(android.R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            // User selects Cancel, discard all changes
                        }
                    }).show();
}

Am I on the right track or is there another way to accomplish what I’m trying to do here? Any help would be great!

  • 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-05-13T23:28:00+00:00Added an answer on May 13, 2026 at 11:28 pm

    You’re not quite on the right track; what you should be doing is overriding onKeyDown() and listening for the back key, then overriding the default behavior:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            // do something on back.
            return true;
        }
    
        return super.onKeyDown(keyCode, event);
    }
    

    If you’re only supporting Android 2.0 and higher, they’ve added an onBackPressed() you can use instead:

    @Override
    public void onBackPressed() {
        // do something on back.
        return;
    }
    

    This answer is essentially ripped from this blog post. Read it if you need long presses, compatibility support, support for virtual hard keys, or raw solutions like onPreIme() etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a relatively simple layout that contains an EditText . The activity itself
I have an Activity that consists of a large scrollview (that contains EditText fields)
I have a FrameLayout that contains several ImageView. On the main activity, I record
I have an activity that launches another activity with startActivityForResult method. I would like
This seems like a simple problem: I have a WF4 activity that guides the
I have JavaScript that is doing activity periodically. When the user is not looking
I have an Activity whose layout is in the file main.xml that contains a
I have an activity(say, MyActivity) that contains fragments, and i am using fragmentTransaction.replace(XX,YY) for
I have a large activity that contains 100 or more buttons. But it's working
My situation is following. I have one Activity that contains three main parts. At

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.