【问题标题】:Cordova back button override default behaviorCordova 后退按钮覆盖默认行为
【发布时间】:2021-10-25 11:10:12
【问题描述】:

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

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

【问题讨论】:

    标签: cordova


    【解决方案1】:

    一旦你覆盖了默认行为,你应该告诉应用如何处理这个事件。 试试这个代码:

    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();
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多