When the framework (0.9.20) includes a back button in the navigation bar automatically, how to you define a function to execute when it is clicked? One use case is to present a popup and ask the user about first saving unsaved data.
@xAlien95 Sure, I like the idea of adding custom attributes for buttons, but the back button is different than the left buttons. The back button is hidden and shown by Ionicās view service, so we should add a way to hook into that functionality.
Having the same problem and looking for a workaround at the moment.
Does it trigger an event that can be caught, it looks like itās calling ionicGoBack($event) on ng-click, any chance to hook in there (doesnāt matter if i have to edit source)?
Btw, as far as I remember this is pretty similar to prepareForSegue in iOS, and itās used very often. You might wanna look into that. Thereās also unwinding segue, etc, and Iām pretty sure all that functionality is necessary, so itās a good reference I suppose.
Based on @erayās solution, what I did was just to override $ionicGoBack in one of my controllers. Before doing so, I grabbed a handle to the default implementation - this can be used to call back into on completion of the custom code, or to deregister the custom handler at some point.
I place this code inside one of my controllers:
// override soft back button
// framework calls $rootScope.$ionicGoBack when soft back button is pressed
var oldSoftBack = $rootScope.$ionicGoBack;
$rootScope.$ionicGoBack = function() {
// implement custom behaviour here
};
My requirement is controller specific, so I deregister the custom handle when the controller exits:
var deregisterSoftBack = function() {
$rootScope.$ionicGoBack = oldSoftBack;
};
More details can be found on my related StackOverflow post:
I have encountered a strange behavior with your solution that either no one else has noticed OR I have implemented it incorrectly. I do admit the latter is probably more likely but for the life of me I canāt figure what I may have done wrong.
In short, it seems once I override the back button on any particular tab, the back button on all other tabs or sub-tabs are also overridden. Thus the backbutton on any other Tab takes me back to the the page where it was originally overwritten at. The $scope.$on(ā$destroyā¦) is not restoring the backbutton functionality for the other tabs.
I responded to your original stackoverflow answer as well as posted another related question on stackoverflow. Would appreciate your (or someones) eyes and assistance to figure out whats going on.
Russel, hi, no idea Iām afraid. I havenāt worked on the platform in over a year. Hopefully youāll get some luck on that stackoverflow post. Perhaps ask your own question and link to that post to try and get viewers from my post across to yours?
Good luck, Iād be interested to read the outcome if you do get an answer.