Renan Rodrigues
Renan Rodrigues

Reputation: 306

Problems with ionic back button

I have a problem with the back button of my application.

Initially I thought that the problem was in Cordova, but I have identified that the problem is actually in Ionic.

I found this code while researching for a solution:

  // Disable BACK button on home
  $ionicPlatform.registerBackButtonAction(function (event) {
    if($state.current.name=="app.home"){
      navigator.app.exitApp();
    }
    else {
      navigator.app.backHistory();
    }
  }, 100);

However, it is giving the following error:

Uncaught ReferenceError: $ionicPlatform is not defined

I am putting that code within a new document called functionAngular.js and I add it at the end of the body tag. As I must inform this function ?

My problem is that:

I want my back button to send the user further back in the navigation stack instead of closing the application instantly.

I am grateful for this help.

Upvotes: 1

Views: 396

Answers (2)

Selva
Selva

Reputation: 338

angular.module('EGLISE')

.run(function($ionicPlatform,$state,$ionicHistory){

 $ionicPlatform.registerBackButtonAction(function (event) {
    if($state.current.name=="app.home"){
      navigator.app.exitApp();
    }
    else {
       $ionicHistory.backHistory();
    }
  }, 100);

});

Please modify your functionAngular.js to the above code.

Upvotes: 1

AdsHan
AdsHan

Reputation: 465

I recommend you first add $ionicPlatform in controller, and in the first controller loaded, test every state (see below) that the back button should have different actions.

    $ionicPlatform.registerBackButtonAction(function () {
        if ($state.current.name == " login (example) ") {
            ionic.Platform.exitApp();
        }
        if ($state.current.name == " main menu buttons (example) ") {               
            // Sample message "want to exit the application?" (YES/NO)                         
            if (YES) {
                $ionicViewSwitcher.nextDirection('back');
                $state.go(' ????');
            };
        };
        if ($state.current.name == " order (example) ") {               
            // Sample message "want to exit the order?" (YES/NO)                         
            if (YES) {
                $ionicViewSwitcher.nextDirection('back');
                $state.go(' ????');
            };
        };                        
    }, 100);  

Upvotes: 1

Related Questions