Javascript backbutton事件监听器覆盖android设备后退按钮 [英] Javascript backbutton event listener overrides the android device back button
问题描述
我使用 cordova 2.6.0
创建了 Android应用
。我在我的应用程序中使用 html
标记和 jQuery $ c实现了
菜单
功能$ c>在与设备的 menubutton
进行交互时切换。但我无法想出达到以下要求,表现得像本机应用程序。
I have created Android app
using cordova 2.6.0
. I have implemented a menu
feature in my app using html
markups and jQuery
which toggles on interacting with device's menubutton
. But I could not figure out to achieve the following requirement, to behave like a native app.
要求
如果菜单,
菜单
应隐藏在按设备的后退按钮
上可见
。如果菜单
不可见,那么 backbutton
现在应该正常运行,也就是说它应该退出
app
或转到后退历史记录
。
The menu
should hide on pressing device's backbutton
if the menu
is visible
. If the menu
is not visible the backbutton
should now act normally, which is, either it should exit
the app
or go to the back history
.
这是我的代码
document.addEventListener('deviceready', function(){
document.addEventListener('menubutton', function(){
//Toggle Menu
//Which is working fine
});
document.addEventListener('backbutton', function(){
if(menu is visible) {
//Hide the menu
//This is also working fine
return false;
}
//BUT the default action of backbutton has gone. It cannot exit the app , neither it brings to back history.
//return true;
//I have also tried to return boolean true , but facing the same problem.
});
}, false);
实际问题
如果我为 backbutton
附加 eventlistener
设备的后退按钮
被禁用,它无法正常工作。
If I attached an eventlistener
for backbutton
the device's Back Button
is disabled, It does not works as normal.
我的问题是
是 document.addEventListener('backbutton',function(){});
过度使用设备的后退按钮?如何摆脱它?
Is document.addEventListener('backbutton', function(){});
over riding the device's back button? How to get rid of it?
这在Android 4.1.2设备上发生
推荐答案
使用侦听器覆盖后退按钮后,它不会执行本机功能。您还必须实现退出行为。
Once you have overridden the back button using the listener, it doesn't perform the native functionalities. You have to implement the exit behaviour as well.
在您的重写方法中,使用以下
In your overriding method, use the following
document.addEventListener('backbutton', function(){
if(menu is visible) {
//Hide the menu
//This is also working fine
return false;
}
else //nothing is visible, exit the app
{
navigator.app.exitApp();
}
});
希望有所帮助。
这篇关于Javascript backbutton事件监听器覆盖android设备后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!