7

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');
    }

  });
Mudasser Ajaz
  • 6,197
  • 1
  • 26
  • 29
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
  • did you check if it comes into `if($state.is('/dashboard') || $state.is('dashboard')){` condition for second time you come to dashboard page ? – Mudasser Ajaz Sep 01 '15 at 20:40
  • @mudasserajaz, do you mean like checking with an alert, console.log or popup or how do you mean? – CommonSenseCode Sep 01 '15 at 20:43
  • 1
    check with simple console.log, if you have problem seeing console log of device, then run `ionic run android -l -c` it will show logs in your command window while running. – Mudasser Ajaz Sep 01 '15 at 20:44
  • Wow thanks didn't new about watching logs, so I implemented a few console logs and it seems that after going to the next page it always applies back button but doesn't re-apply exit button functionality :/ – CommonSenseCode Sep 01 '15 at 20:52
  • 1
    so it is going into your `if` statement everytime but still does not work? – Mudasser Ajaz Sep 01 '15 at 20:55
  • @mudasserajaz, it doesn't re run the if statement of **`dashboard`** so it keeps the go back functionality – CommonSenseCode Sep 01 '15 at 20:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88515/discussion-between-mudasser-ajaz-and-user912010). – Mudasser Ajaz Sep 01 '15 at 20:57

1 Answers1

20

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.

Mudasser Ajaz
  • 6,197
  • 1
  • 26
  • 29