Reputation: 3322
I want to show alert when user presses home button on device(do you want to exit the app ?) How to do it ?
Upvotes: 0
Views: 1847
Reputation: 2138
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
@Override
public void onBackPressed() {
// your dialog here
}
Upvotes: 4
Reputation: 2198
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity
.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Upvotes: 0
Reputation: 559
Write the show alert dialog code in onPause() it will work perfectly.
Upvotes: -2