如何在系统导航通过手势时调用 onUserLeaveHint?

huangapple go评论82阅读模式
英文:

How to call onUserLeaveHint when the system navigation are by gestures?

问题

在此时,我正在为Android实现画中画功能,根据文档的说法,当用户通过Home按钮离开应用时,我们应该使用onUserLeaveHint()方法进入画中画模式。在使用物理按钮的设备上,它按预期工作,但当手机没有物理按钮并使用类似iPhone的手势时,onUserLeaveHint方法不会被调用。那么,有没有办法捕捉或模拟这种行为?

英文:

at this moment I am implementing the picture in picture feature for android, as the documentation says we should enter in the picture in picture mode when the user leaves the app through the home button with the method onUserLeaveHint() it works as expected in devices with physical buttons, but when the phone has not and work with gestures like the iPhone the method onUserLeaveHint is not called, so is there a way to catch or simulate that behavior?

答案1

得分: 1

onUserLeaveHint() 不是一个可靠的 Android 生命周期回调来检测“Home”按钮点击。尽管官方文档指定可以使用它来检测“用户按下Home键”时,我发现这是不准确的。

例如,onUserLeaveHint() 可能在触发 Android 原生对话框或启动另一个 Activity 时被调用。此外,就你提到的例子而言,我还发现当启用手势导航(Android 11及以上版本)并且用户进入“最近使用应用”然后退出应用时,onUserLeaveHint() 不会被调用。

不幸的是,直到今天我还没有找到任何可靠的方法来检测 Android 中的“Home”按钮点击。我猜操作系统设计师可能不希望应用开发者轻松地进行此类检测。

英文:

onUserLeaveHint() is not a reliable Android lifecycle callback to detect "Home" button clicks. Even though the official documentation specifies that you can use it to detect "when the user presses the Home key", I have found this to be false.

For example, onUserLeaveHint() might be called when the an Android native dialog is prompted, or when you start another Activity.
Also, as for the example you have mentioned, I have also found that when Gesture Navigation is enabled (Android 11 and above) and the user goes to "Recents" and then exists app - onUserLeaveHint() will not be called.

Unfortunately, until this day I have not found any air-tight way to detect "Home" button clicks in Android. I guess that OS designers do not want app developers to detect this easily.

huangapple
  • 本文由 发表于 2020年7月31日 08:58:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63184333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定