Disable hardware back button in Ionic application?

后端 未结 8 836
暗喜
暗喜 2020-12-24 13:21

I\'m trying to disable the back button on my Cordova app. I\'m using AngularJS + Ionic Framework. I found topics about this and tried the code bellow, but it has absolutely

相关标签:
8条回答
  • 2020-12-24 14:12

    The example in the docs shows the event listeners — even the deviceready — being attached after the document onload event has fired.

    Using your code:

    function onDeviceReady() {
      document.addEventListener("backbutton", function (e) {
        e.preventDefault();
        console.log("hello");
      }, false);
    }
    
    document.onload = function () {
      document.addEventListener("deviceready", onDeviceReady, false);
    };
    
    0 讨论(0)
  • 2020-12-24 14:14

    Finally found the answer on this Ionic Forum thread:

    $ionicPlatform.registerBackButtonAction(function () {
      if (condition) {
        navigator.app.exitApp();
      } else {
        handle back action!
      }
    }, 100);
    

    $ionicPlatform.registerBackButtonAction allows to completly overwrite back button behavior. First param is a callback function and the secondone a priority (only the callback with the highest priority gets executed).

    0 讨论(0)
提交回复
热议问题