How to add Back button to Home


#1

Ionic has a different navigation when press a function from side menu. When you press a back button from handset, it will straight away exit.

I refer to this tutorial and make the changes to my app.js but it unable to back to home page but exit the app.

The code suppose to (which is what I needed):

1.if not at home page, it will back to previous page if press hardware back button
2.if it is at home page, press hardware back button will show pop up to confirm if user really want to exit.

app.js

angular.module('myapp', ['ionic', 'myapp.controllers', 'myapp.factories'])
.run(function($ionicPlatform,$state,$ionicHistory,$ionicPopup) {
  $ionicPlatform.ready(function() {
if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }

    $ionicPlatform.registerBackButtonAction(function (event) {
    event.preventDefault();
    if ($state.current.name == "app.home") {
      var confirmPopup = $ionicPopup.confirm({
        title: 'Exit',
        template: 'Confirm Exit'
      });

      confirmPopup.then(function (res) {
        if (res) {
          navigator.app.exitApp();
        }

      });
    } else {
      $ionicHistory.nextViewOptions({ disableBack: true });
      $state.go('app.home');
    }
  }, 100);//registerBackButton

});
})

  $stateProvider
      .state('app', {
        url: '/app',
        abstract: true,
        templateUrl: 'templates/menu.html',
        controller: 'menuCtrl'
      })
      .state('app.home', {
        url: '/home',
        views: {
          'menuContent': {
            templateUrl: 'templates/home.html',
            controller: 'homeCtrl'
          }
        }
      })
      .state('app.personalitem', {
        url: '/personalitem',
        views: {
          'menuContent': {
            templateUrl: 'templates/personalitem.html',
            controller: 'personalitemCtrl'
          }
        }
      })
      .state('app.pStock', {
        url: '/pStock',
        views: {
          'menuContent': {
            templateUrl: 'templates/pStock.html',
            controller: 'pStockCtrl'
          }
        }
    });
angular.module('myapp.factories', []);
angular.module('myapp.controllers', []);