0
votes

I used Ionic in my app, but when I try to press the physical back button, it should exit the app, rather than back to last state.

I listened to $ionicPlatform.registerBackButtonAction event, and it does not execute.

app.js

app.run(function ($rootScope, $cordovaToast, $ionicPlatform, $state, $cordovaKeyboard) {
  var deregister;
  $ionicPlatform.ready(function () {

if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
  cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  cordova.plugins.Keyboard.disableScroll(true);

}
if (window.StatusBar) {
  StatusBar.styleDefault();
}
$ionicPlatform.registerBackButtonAction(function (event) {
  event.preventDefault();
  console.info('天啦噜,你终于执行了')
  if ($state.current.name == "tab.discover") {
    console.info('哈哈,要退出了')
    navigator.app.exitApp();
  }
  else {
    console.info('哈哈,要返回了')
    navigator.app.backHistory();
  }
}, 101);

});

1
Does this work with ionic serve, i.e. is the problem really android-specific? - Eric Gopak

1 Answers

0
votes

Since it's going back a page, problem is most likely with $state.current.name, console.log($state.current.name) to see if its what it should be and use three === instead of == to make more accurate. If that's not the case try changing the 101 to 100 in the registerBackButtonAction.