Handling Android back button

Though I have posted the same issue in Ionic forum before but didn’t get any response.

I am having trouble dealing with Android’s back button with Ionic and have tried a number of possible solutions but no one has worked yet. Currently I am using this:

.run(function($ionicPlatform, $rootScope) {
   

 $ionicPlatform.ready(function() {
        if(window.cordova && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
            StatusBar.styleDefault();
        }
            
        $rootScope.colors = ["#9798b4", "#99b3d4", "#4d5987", "#3aa36a","#F0717B", "#50679C", "#3BA46B", "#D48C52", "#E94B43", "#F0DDB9",
                            "#396F7F", "#FFCB49", "#07BC9F", "#8E7261", "#34495C", "#ECF0F1", "#F0717B", "#50679C"];
        
    });
    
    $ionicPlatform.registerBackButtonAction(function () {
       alert("Hello");
     }, 1000);
    
    })

Please help me with this issue.

You can try:

$ionicPlatform.onHardwareBackButton(function() {
       //code goes here
});

But whatever you have done should be working.