Original posted here, but as @Gajotres pointed out to start a new topic.
Hello.
I am trying to find out a way such that when the hardware back button is pressed, if I am on the application’s main page, the application will simply exit. But if it is on the other pages, then it would simply navigate back to each page until it reaches the main (home page).
Currently, what’s happening is that when I am at home page, I click on button and goes to page1. I click on the home button to go back to home.html. Then I would go to page2.html all the way to sub-page-c.html. Now when I click the back button, I go from sub-page-c to page2 to home then to page1.html. What I want to achieve is whenever I am already at home.html, the back button will simply correspond to an exit application.
Side Question: Just wondering, if you are using the Chrome Debugger and using the emulator for the application, is the browser back button the same as the back button for the hardware (Android device)? Or would it still be preferable to generate an APK and load it to your device and test?
The Ionic framework uses the angular-ui-router module for handling routes and defining states (including nested states and nested views). So, it would be good if you could phrase your question in terms of the states you have defined and wish to transition from/to.
$ionicPlatform.registerBackButtonAction(function(event) {
if ($ionicHistory.backView() === null) { // no more previous screen in the history stack, so "back" would exit
$ionicPopup.confirm({
title: 'Plase confirm',
template: 'Are you sure you want to exit the app?'
}).then(function(res) {
if (res) {
ionic.Platform.exitApp();
}
})
} else {
$ionicHistory.goBack();
}
}, 100); // 100 = previous view
Well, I have to say that for me it also does not work perfect, yet. In some cases the back button does what I want and in some cases it does nothing. The whole navigation and history stuff is quite complicated.
But, before putting in this code it would quit my app and now it doesn’t do that anymore, I get a nice warning now so that’s already an improvement. When I get to know more or have more improvements I’ll let you know.