1

i'd like to control the event of "press hardware back button" in ionic framework. This is my code. Why does not it work? thank you

.run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }

            $ionicPlatform.registerBackButtonAction(function (event) {
                event.preventDefault();
                event.stopPropagation();
                alert("Stop");
            }, 100);
        });
    })

i also try the other solution:

    .run(function ($ionicPlatform) {
        $ionicPlatform.ready(function () {
            if (window.StatusBar) {
                StatusBar.styleDefault();
            }
        });

        $ionicPlatform.registerBackButtonAction(function (event) {
                event.preventDefault();
                event.stopPropagation();
                alert("Stop");
            }, 100);
    })

but it still doesn't work

thank you

Belsen
  • 313
  • 3
  • 4
  • 17

2 Answers2

1

that's what you want ? :

$ionicPlatform.registerBackButtonAction(function(e){
    e.preventDefault();
    return false;
}, 101);

credit to : mircobabini/Angular.Ionic.HardwareBackButtonManager.js https://gist.github.com/mircobabini/689955216a036544d594

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255
harksin
  • 235
  • 2
  • 11
  • 1
    Doesn't work for me. I'm doing a very small webapp that has a flow of three steps, in which when you go to the next step, you should not be able to go back. When I click on an item with ```ng-href="#/another page"```, then press the physical back button, it still goes back. Any other suggestions? I'm almost thinking in rewriting the ```$stateProvider``` call on ```app.js``` with a couple of booleans so that it will not go back to the previous state, manually. – Nighto Aug 10 '15 at 22:19
  • I also tried [this](http://stackoverflow.com/questions/30475604/ionic-angular-ui-routing-how-to-delete-back-stack?rq=1) with no luck. – Nighto Aug 10 '15 at 22:23
1

In a cordova based application (Ionic) the following three methods can be used to override hardware back button.

  • onHardwareBackButton
  • offHardwareBackButton
  • registerBackButtonAction

Look at http://www.codeexpertz.com/blog/mobile/override-hardware-back-button-cordova-based-application-ionic for details

Anishnirmal
  • 540
  • 5
  • 11