Javascript backbutton事件监听器覆盖android设备后退按钮

Lek*_*ath 12 javascript jquery android back-button cordova

我创建了Android app使用cordova 2.6.0.我已经menu在我的应用程序中使用html标记实现了一项功能,并jQuery在与设备进行交互时切换menubutton.但我无法想出达到以下要求,表现得像本机应用程序.

需求

如果是,menu应隐藏在按下设备上.如果不可见,那么现在应该正常行动,也就是它应该是或应该去.backbuttonmenuvisiblemenubackbuttonexitappback 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);
Run Code Online (Sandbox Code Playgroud)

实际问题

如果我eventlistenerbackbutton设备附加了一个Back Button已禁用的设备,则它不能正常工作.

我的问题是

是否document.addEventListener('backbutton', function(){});超过设备的后退按钮?如何摆脱它?

这是在Android 4.1.2设备上发生的

SHA*_*ANK 10

使用侦听器覆盖后退按钮后,它不会执行本机功能.您还必须实现退出行为.

在您的重写方法中,使用以下方法

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();
  }
});
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.