Android的onBack pressed / onUserLeaveHint [英] Android onBackPressed/onUserLeaveHint

查看:96
本文介绍了Android的onBack pressed / onUserLeaveHint的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是有点需要真正的意见。我有我的比赛一个活动运行,并在用户presses后退按钮将退回到主菜单使用onBack pressed()方法,但我也覆盖onUserLeaveHint()来做同样的动作,如果home键是pssed $ P $或者接到电话。然而,这种方法也被称为当返回按钮是pressed,这意味着在主菜单意图被调用两次,一个在另一个的顶部上。

Just a bit of advice needed really. I have an Activity running with my game in it and when the user presses the Back button it will exit back to the Main Menu using the onBackPressed() method, but I am also overriding the onUserLeaveHint() to do the same action if the Home Button is pressed or a phone call is received. However this method is also called when the Back button is pressed, meaning that the Main Menu Intent is called twice with one on top of the other.

如果任何人有关于如何解决这个问题的一个想法或处理的两个事件一个更好的方式,它会大大AP preciated。

If anyone has an idea about how to get around this issue or a better way of handling the two events it would be much appreciated.

感谢。

推荐答案

onUserLeaveHint()是一个受保护的方法作为活动的其他生命周期方法,如果你正在处理onUserLeaveHint这会照顾以下情况

onUserLeaveHint() is a protected method as other lifecycle methods of the activity and if you are handling onUserLeaveHint this will take care of the following case

  1. 当用户点击Home键
  2. 当用户点击返回键
  3. 当用户点击报警吧

基本上它有预兆用户试图离开你的活动。这意味着如果你正在处理 onUserLeaveHint()则不需要办理 onBack pressed()的您的code。

Basically it hints about the user is trying to leave your activity. That means if you are handling onUserLeaveHint() you don’t need to handle onBackPressed() in your code.

这篇关于Android的onBack pressed / onUserLeaveHint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆