I want to be able to override the BACK button on the navigation bar, and the hardware button.
I want this override to be for one specific controller, but not for the rest of the controllers.
- it must be cancelled when the user moves to another screen
(using ionic v1.0.0 uranium-unicorn)
My reason is I have a list of items. Clicking on the list opens a details page, with 3 tabs. Each tab shares the same controller.
However, pressing BACK on any of those tabs must go back to the main list. That is how it works on native devices, so that is how I would like it to work on my hybrid app.
Many solutions provided online seem to be for older beta versions, or for registration outside of controllers.
A common solution for working with the Android hardware button inside a controller is:
$ionicPlatform.registerBackButtonAction(function (event) {
if($state.current.name=="home"){
alert("button back");
}
}, 100);
However this doesn't seem to work on the soft navigation bar button, and it works across all controllers, not just the one.
are you talking about the soft navigation as in the back button on the ion-header-bar or ion-nav-bar? cause that is a easy fix. Just make your own custom header bar for that template. so on that state template just use something like this.
I took Richard's suggestion and put it into a service to make it more reusable.
The Controller
The Service
Took the feedback from @raven.zuo and made some amends to un-register the state change event.
this is my solution :)
Put this part of code in your app.js run function :
Now, you are able to create a custom goback function in controllers :
This function will be automatically called when user tap on nav-back-button.
If you want to call goBack by yourself, you can do it by this way :
If you want to bypass the custom function whereas it is declared, here you go :
You can assign different behavior for the back button directly in each controllers :)
It is possible to override both buttons in your controller, without any changes the the HTML code.
To summarise:
$rootScope.$ionicGoBack()
$ionicPlatform.registerBackButtonAction()
Detailed explanations below.
The solution for overriding the soft navigation bar BACK button comes from understanding what Ionic does when that button is pressed.
From the Ionic docs for
ion-nav-back-button
, we already know that:Searching the source code in ionic.bundle.js reveals how this is declared:
Overriding this in your own controller is simple. Make sure you pass
$rootScope
into the controller and just modify the above code. It is a good idea to grab a pointer to the original function so you can restore it if required, or call into it when finished with your custom processing.The solution for overriding the Android hardware BACK button, for only one controller, comes from the return value of the
registerBackButtonAction()
function, which does the deregistration of the override.Call that deregistration method in the
$scope.$on('$destroy'...
handler.More details here:
A full solution would require the following:
The following code illustrates how this can be done:
This issue has been discussed on the Ionic forums & issues pages:
The above answer to override $rootScope.$ionicGoBack partially works.
The problem is with the way of deregisterSoftBack. I tried the above mentioned $scope.$on('$destroy', a_function) and also the new $scope.$on('$ionicView.beforeLeave', a_function), neither works.
The reason for it: the new controller will be entered before the deregisterSoftBack thus make the deregister fail. So I modified the solution a bit to make it work.
change the
to
deregister in $rootScope.$on("$stateChangeStart", your_function), the code is: