[英]Javascript backbutton event listener overrides the android device back button
I have created Android app
using cordova 2.6.0
. 我使用
cordova 2.6.0
创建了Android app
。 I have implemented a menu
feature in my app using html
markups and jQuery
which toggles on interacting with device's menubutton
. 我在我的应用程序中使用
html
标记和jQuery
实现了一个menu
功能,它可以切换与设备的menubutton
进行交互。 But I could not figure out to achieve the following requirement, to behave like a native app. 但我无法想出达到以下要求,表现得像本机应用程序。
Requirement 需求
The menu
should hide on pressing device's backbutton
if the menu
is visible
. 如果
menu
visible
, menu
应隐藏在按设备的backbutton
按钮上。 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
. 如果
menu
是不可见的backbutton
现在应该正常行为,这是,无论是它应该exit
的app
或去back history
。
This is my code 这是我的代码
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);
The actual problem 实际问题
If I attached an eventlistener
for backbutton
the device's Back Button
is disabled, It does not works as normal. 如果我附加了
eventlistener
的backbutton
设备的Back Button
被禁用,它不正常的结果。
My question is 我的问题是
Is document.addEventListener('backbutton', function(){});
是
document.addEventListener('backbutton', function(){});
over riding the device's back button? 超过设备的后退按钮? How to get rid of it?
如何摆脱它?
This is happening on Android 4.1.2 Device 这是在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();
}
});
Hope that helps. 希望有所帮助。
To answer your question: 回答你的问题:
Is document.addEventListener('backbutton', function(){});
是document.addEventListener('backbutton',function(){}); over riding the device's back button?
超过设备的后退按钮? How to get rid of it?
如何摆脱它?
You can as well remove the event listener on page redirect to continue using the native functionality of back button in subsequent pages. 您还可以删除页面重定向上的事件侦听器,以继续使用后续页面中后退按钮的本机功能。 Code to remove event listener as follows:
删除事件侦听器的代码如下:
document.removeEventListener("backbutton", onBackButton, false);
where onBackButton is the function associated with the back button event. 其中onBackButton是与后退按钮事件关联的函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.