tags:

views:

1336

answers:

1

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!

+7  A: 

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: http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Daniel Lew
Using the onKeyDown() method will have the same issues as onPause(). If I attempt to show an alert dialog in the onKeyDown block when I detect the Back keycode the dialog still will not block. I'll see the alert momentarily until the super.onKeyDown is pressed which will cause the window to be closed. Is there a way to force a response to the AlertDialog before continuing?
chriskopec
Are you calling super.onKeyDown() if you consume the back button? I assume that you should just be using "return true" if you pop open the dialog, in order to indicate to the system that you've consumed the back button press; otherwise, by calling super(), you'll still end calling all the things that finish the activity.
Daniel Lew
That's it, Thanks!
chriskopec