SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

How to show alert Dialog when home button is pressed ?(ICS)

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

Answers (3)

Dimmerg
Dimmerg

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

NasaGeek
NasaGeek

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

Android Overriding home key

Upvotes: 0

user8938
user8938

Reputation: 559

Write the show alert dialog code in onPause() it will work perfectly.

Upvotes: -2

Related Questions