registerBackButtonAction() method is not triggered on hardware back button Android

Hi,

I have written a piece of code to override the hardware back button functionality.
This was working earlier in this project, also its still working in my other project.

Don’t know exactly what’s the issue.

$ionicPlatform.registerBackButtonAction(function (e) {
alert(‘triggered’);
if($state.is(‘app.home’)){
e.preventDefault();
}
else if($state.is(‘app.login’)){
$ionicPopup.confirm({
title:‘Exit’,
template:‘Are you sure you want to exit?’
})
.then(function(res){
if(res){
ionic.Platform.exitApp();
}
else{
//some code
}
})
}
else{
$ionicHistory.goBack();
}
}, 100);

The registerBackButtonAction() method is never triggered and the alert for triggered is not executed on click of hardware button.

Please help

Thanks

you can use
document.addEventListener(“backbutton”,onBackButton, true);
and write onBackButton function to manage the activities as per your requirement

Thanks, but i cannot close the ionicPopup with the backbutton now.
The view in the background routes with the back button but the active popup is not closed.

Please help.

Thanks

try writing pop close code in the back button event