Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  });
like image 379
CommonSenseCode Avatar asked Sep 01 '15 20:09

CommonSenseCode


1 Answers

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.

like image 93
Mudasser Ajaz Avatar answered Sep 21 '22 00:09

Mudasser Ajaz