Back button handle


#1

Hi, im new in ionic, im trying handle the back button:

var AppPanico = angular.module(“AppPanico”,[“ionic”]);

AppPanico.controller(“AppCtrl”, ["$scope", “$log”, AppCtrl]);

function AppCtrl ($scope, $log)
{

$scope.onHardwareBackButton(function () {alert(“test”)});

…some code…

}

Any idea?
Thanks!!


#2

I change my code:

AppPanico.controller('AppCtrl', function($scope, $ionicPlatform,  $state) {
    
$ionicPlatform.onHardwareBackButton(function () {
event.preventDefault();
event.stopPropagation(); 
alert("test");
return;
});

But nothing :frowning:


#3

Are you trying to prevent the default behavior here?
Try this

       var deregister = $ionicPlatform.registerBackButtonAction(
                function () {
                    alert("Show on the home page")
                }, 100
        );
        $scope.$on('$destroy', deregister)

#4

Hi, i try but the app still closing :frowning:

In the index.html:

<body data-ng-controller="AppCtrl as app">

#5

Hi!! I test with ripple, works!
But in the device when i press the back button the app close, maybe something with android i dont know


#6

Hmm, try lowering the priority number,

 var deregister = $ionicPlatform.registerBackButtonAction(
                function () {
                    alert("Show on the home page")
                }, 1
        );
        $scope.$on('$destroy', deregister)

This way it executes at a higher priority than exiting the app