Ionic cancel hard BACK button override

后端 未结 1 504
闹比i
闹比i 2021-01-20 13:07

There are questions about overriding the physical Android BACK button in Ionic, to provide custom behaviour:

  • Ionic override all BACK button behaviour for speci
相关标签:
1条回答
  • 2021-01-20 14:03

    Ionic v1 solution (out of date)


    According to the Ionic docs for $ionicPlatform, the registerBackButtonAction() returns:

    A function that, when called, will deregister this backButtonAction.

    This can be seen in the code for registerBackButtonAction():

      // return a function to de-register this back button action
      return function() {
        delete self.    [action.id];
      };
    

    So the correct way to deregister / cancel the custom behaviour is to call that function when the controller is destroyed:

    var customBackButton = function() {
        console.log("this is custom behaviour");
    };
    
    // registerBackButtonAction() returns a function which can be used to deregister it
    var deregisterBackButtonAction = $ionicPlatform.registerBackButtonAction(
        customBackButton, 101
    );
    
    $scope.$on('$destroy', function() {
        deregisterBackButtonAction();
    });
    

    A more complete example showing how to override & restore the hard and soft buttons can be found here:

    • Ionic override all BACK button behaviour for specific controller
    0 讨论(0)
提交回复
热议问题