Cordova 后退按钮覆盖默认行为

Cordova back button override default behavior

我已经覆盖了后退按钮的 Cordova 默认行为,所以如果有任何 dialogs/modals 打开关闭它们(本机行为),但如果没有,那么我希望后退按钮的默认导航到设置,我试图只设置 window.history.back() 但它似乎不是默认设置,例如:如果您多次单击后退,它不会关闭应用程序。

      document.addEventListener("backbutton", function(event){
                 if (dialogService.openDialogs.length > 0) {
                     dialogService.closeAll();
                     event.preventDefault();
                 } else {
                     window.history.back();
                 }
         })

覆盖默认行为后,您应该告诉应用如何处理此事件。 试试这个代码:

document.addEventListener("backbutton", function(event){
  if (dialogService.openDialogs.length > 0) {
     dialogService.closeAll();
     event.preventDefault();
  } else if (document.referrer == "") { //you check that there is nothing left in the history. It will work most of the time in cordova apps but this is not a perfect way to check it    
    navigator.app.exitApp(); //when you override the default behavior, this is the command to close the app
  } else {
     window.history.back();
  }
});