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?
Asked
Active
Viewed 199 times
0
-
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
-
1There 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 Answers
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
-
+1 correct answer. @drew: Just don't forget to call `finish()` on clicking "Yes" button. – Eng.Fouad May 05 '12 at 17:33