0

When the user clicks the back button on the main activity, I would like a dialog to pop up asking the user if he "wants to leave". I am able to create the dialog and implement it but I am unsure of how to make the dialog open when the back button is pressed. Any Suggestions?

Adam Liss
  • 47,594
  • 12
  • 108
  • 150
drew
  • 131
  • 2
  • 6
  • The same question was already answered here: http://stackoverflow.com/questions/6290599/prompt-user-when-back-button-is-pressed – noyb May 05 '12 at 17:32
  • 1
    There are many ways a user can "leave" your main activity -- BACK, HOME, incoming phone call, responding to a `Notification`, etc. BACK is only one of these. Whatever you do to make sure the user does not lose any in-progress work when a phone call comes in is an equally good plan to do if they hit the BACK button. – CommonsWare May 05 '12 at 17:53

1 Answers1

1

Which API? This is the easiest way from API 5+

@Override
public void onBackPressed () {
    //super.onBackPressed();
    dialog.show();
}
Sam
  • 86,580
  • 20
  • 181
  • 179