Override android hardware back button, make it exit in one page and make it back in the next pages

CommonSenseCode picture CommonSenseCode · Sep 1, 2015 · Viewed 10.6k times · Source

I'm using the following controller so that in dashboard page android back button exits app but on the rest of pages it goes back. Since before dashboard page I had a tutorial I only present once to my users, then I had to override android back button so that in dashboard it exits if pressed, it works with this code fine:

angular

  .module('az-app')
  .controller('DashboardController', function ($scope, $state, $ionicPlatform) {

    /**
     * While user on dashboard.html we don't want Android back button to return
     * to tutorial views so we override it so that in case that back button is pressed
     * to exit app which is in accordance with android lifecycle.
     *
     */
    $ionicPlatform.registerBackButtonAction(function () {
       if($state.is('/dashboard') || $state.is('dashboard')){
        navigator.app.exitApp();

       }
    }, 100);


  });

Now the problem is that when I go to the following views it still works as an exit button, but I want it to be just a back button in any other view that isn't dashboard, so I tried this in the following controller:

angular

  .module('az-app')
  .controller('DirMedicoController', function ($scope, $state, $ionicPlatform) {

    $ionicPlatform.registerBackButtonAction(function () {

      navigator.app.backHistory();

    }, 100);

  });

So now it gives back functionality, but then again when at dashboard if I press back coming from the past controller it overrides its functionality and now instead of exiting it goes back.

UPDATE

Thanks to answer below by mudasser ajaz I could finally made it work answer is:

angular

  .module('az-app')
  .controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {

    /**
     * While user on dashboard.html we don't want Android back button to return
     * to tutorial views so we override it so that in case that back button is pressed
     * to exit app which is in accordance with android lifecycle.
     *
     * Else if not on dashboard just work as normal back button
     *
     */
    $ionicPlatform.registerBackButtonAction(function() {
      if ($location.path() === "/dashboard") {
        navigator.app.exitApp();
      }
      else {
        $ionicHistory.goBack();
      }
    }, 100);


    $scope.backToPolicy = function () {
      $state.go('intro');
    }

    $scope.showDirMedico = function () {
      $state.go('dirmedico');
    }

  });

Answer

Mudasser Ajaz picture Mudasser Ajaz · Sep 1, 2015

Do this in your dashboard controller

$ionicPlatform.registerBackButtonAction(function() {
//var path = $location.path()
  if ($location.path() === "/dashboard" || $location.path() === "dashboard") {
    navigator.app.exitApp();
  }
  else {
    $ionicHistory.goBack();
    //navigator.app.goBack();
  }
}, 100);

And add $location and $ionicHistory as dependency

.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {

Remove registerBackButtonAction from other controller.