How can I show a exit app confirm when on login or home page?

I want to show a exit app confirm modal on login or home page when user press android hardware back button, but on other pages just back to previous page, I have trouble in the funtion, I know $ionicPlatform.registerBackButtonAction , but how can I know current location?

I have wrote blow code in my LoginControl , it can show confirm modal on login and home page , but can not back to previous in other page.

        var exitConfirm = function () {
        if ($location.path() == '/login' || $location.path() == '/main/recharge') {
            $ionicPopup.confirm({
                title: 'Exit App',
                content: 'Do you want to exit?',
                okText: 'OK',
                cancelText: 'Cancel'
            }).then(function (res) {
                    if (res) {
                        navigator.app.exitApp();
                    }
            });
        }
    }

    var removeConfirm = angular.noop();
    if ($location.path() == '/login' || $location.path() == '/main/recharge') {
         removeConfirm = $ionicPlatform.registerBackButtonAction(
            exitConfirm,
            100, 111111111111
        );
    } else {
        removeConfirm();
    }

Here is what I use, which is adapted from http://forum.ionicframework.com/t/press-back-button-again-to-exit/4725:

$ionicPlatform.registerBackButtonAction(function(e) {

  e.preventDefault();

  function showConfirm() {
    var confirmPopup = $ionicPopup.confirm({
      title: '<strong>Exit X-Wing Companion?</strong>',
      template: 'Are you sure you want to exit XWC?'
    });

    confirmPopup.then(function(res) {
      if (res) {
        ionic.Platform.exitApp();
      } else {
        // Don't close
      }
    });
  }

  // Is there a page to go back to?
  if ($rootScope.$viewHistory.backView) {
    // Go back in history
    $rootScope.$viewHistory.backView.go();
  } else {
    // This is the last page: Show confirmation popup
    showConfirm();
  }

  return false;
}, 101);

I put this in the .run() call in app.js. Make sure to add $ionicPopup and $ionicPlatform as arguments, like so: .run(function($rootScope, $ionicPopup, $ionicPlatform) {

Hope this helps.

3 Likes

Done! You answer help me solved this problem.
Thank u.

Glad I could help :slight_smile:

@offbye It’s not working for me :frowning:
It disable the back button but not show confirm.

.run(function($ionicPlatform, $ionicPopup, $rootScope, $location) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleLightContent();
    }

    //主页面显示退出提示框
    $ionicPlatform.registerBackButtonAction(function(e) {

      e.preventDefault();

      function showConfirm() {
        var confirmPopup = $ionicPopup.confirm({
          title: '<strong>退出应用?</strong>',
          template: '你确定要退出应用吗?',
          okText: '退出',
          cancelText: '取消'
        });

        confirmPopup.then(function(res) {
          if (res) {
            ionic.Platform.exitApp();
          } else {
            // Don't close
          }
        });
      }

      // Is there a page to go back to?
      if ($location.path() == '/tab/locations') {
        showConfirm();
      } else if ($rootScope.$viewHistory.backView) {
        console.log('currentView:', $rootScope.$viewHistory.currentView);
        // Go back in history
        $rootScope.$viewHistory.backView.go();
      } else {
        // This is the last page: Show confirmation popup
        showConfirm();
      }

      return false;
    }, 101);
  });
})

I found what I want in Press back button again to exit

@emj365 , You can use $ionicHistory.backView() , its working for me.

If still some one facing same issue, can find full example here:

not working in my app. app closed without ionicpopup while clicking on the back button

did u find any solution?