Back button handle

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!!

I change my code:

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

But nothing :frowning:

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)

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

In the index.html:

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

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

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