1

I'm implementing this application where i need to pause it when the back key is pressed or show a dialog to ask the user if he really wants to exit.

I did override the back key to show a dialog with 2 button, yes and cancel, but the activity is finished anyways without showing the dialog

the code I am using to override the back key is the following

// Overriding The Back Key To Stop The Music If Running
@Override
public void onBackPressed() {

    super.onBackPressed();
    show_are_you_sure_exit();
}

and the function is the following

public void show_are_you_sure_exit() {
    final AlertDialog show_are_you_sure_delete = new AlertDialog.Builder(
            this).create();
    show_are_you_sure_delete
            .setMessage("Are You Sure You Want To Exit?! Your Pregress Won't Be Saved");
    show_are_you_sure_delete.setButton("Yes",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface StopActivityDialog,
                        int which) {
                    start_main_screen_intent();
                    finish();
                }
            });
    show_are_you_sure_delete.setButton2("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            });

    show_are_you_sure_delete.show();

}

with start_main_screen_intent() starts another activity

thx for your help :)

wassim
  • 277
  • 1
  • 6
  • 22

3 Answers3

1

your are calling super.onBackPressed() which dispatches the event up and kills the activity what you want is

@Override
public void onBackPressed() {
    show_are_you_sure_exit();
}

this way YOU handle the event your way, not the normal way

vanleeuwenbram
  • 1,289
  • 11
  • 19
0

Try removing the super.onBackPressed() call. You may also need to remove the @Override in this case or Eclipse might yell at you. I think what is happening is the base implementation is ending the activity.

Joe
  • 2,649
  • 21
  • 33
  • don't remove the "@Override" annotation because this can be a useful check for typo's if you have "@Override" but you are not overriding then eclipse will complain (as it should) – vanleeuwenbram Nov 29 '11 at 21:47
0

Don't call super.onBackPressed();. The only thing it does (if you extend Activity) is it calls finish().

However, note this 'exit' feature is smth not from Android world. I'd recommend to review your app architecture to get rid of exit concept.

You can find details on why 'exit' is bad in this post: Is quitting an application frowned upon?

Community
  • 1
  • 1
Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91