Android - 当用户按下主页按钮时如何退出应用程序?
我想知道当用户按下主页按钮时如何退出应用程序。
据我所知,主页按钮将正在运行的应用程序移动到后台并将启动器进程放在前面。
我知道我可以使用 finish() 但我不知道应该在哪里调用它,因为我不知道当用户按下 Home 键时哪个函数将被调用。
谢谢。
I want to know how to exit an app when user press the Home Button.
As far as i know that Home Button moves the running app in background and puts Launcher process in front.
I know i can use finish() but i don't know where i should call it because i have no idea which function is going to get a call when user will press the Home Key.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您想要执行的操作,覆盖
onUserLeaveHint
可能是最好的选择:http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
这会让您的应用程序知道您的应用程序正在退出,因为用户选择了切换应用程序(例如通过点击“主页”按钮或选择通知)。如果来电(或类似情况)导致您的应用程序转到后台,则将不会调用此提示函数,因为用户没有启动该操作。
Depending on what you want to do, overriding
onUserLeaveHint
might be the best bet:http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
This will let your app know that your app is being exited because the user chose to switch apps (Like by hitting the Home button or selecting a notification). This hint function will not be called if an incoming phone call (or similar) is causing your app to go to the background, as the user didn't initiate that action.
就我而言,
我有很多应用程序活动
因此,如果我使用“onUserLeaveHint”、“onStop”或...
它只会捕获活动转换。
所以我把每个活动的代码放在下面。
最后捕获用户的主页按钮操作。
首先,您有单例对象
和每个活动,
我希望这对您有帮助
In my case,
I have many activities in application
so if I use 'onUserLeaveHint', 'onStop' or ...
It just catch activity transition.
So I put below code every activities.
And finally catch user's home button action.
first, you have singleton object
and every activities,
I hope this will help you