简体   繁体   中英

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

我想在用户按下设备上的主页按钮时显示警告(你想退出应用程序吗?)怎么做?

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
}

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

在onPause()中编写show alert对话框代码,它将完美地工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM