Clear navigation history stack


#1

How do I clear navigation history stack? Can I achieve that?


#2

You should be able to use $ionicHistory.clearHistory() but I haven’t personally used it. I use the following code to set the next view to the root, like when I am going to the home view.

$ionicHistory.nextViewOptions({
     disableAnimate: true,
     historyRoot: true
});

#3

I tried that but it did not work.
I used $ionicViewService.clearHistory() and it worked.
Has this been changed?


#4

Oh sorry. $ionicViewService was deprecated in beta 14. I assumed you were using the most recent version.


#5

Thanks!
Another thing: how can I set a back button function in a view to always go back to a fixed specified view?


#6

You could you override the ng-click function of the back button? I still don’t know what version of Ionic you’re using.

<ion-nav-back-button class="button-clear"
ng-click="goBack()">
</ion-nav-back-button>

Then in the goBack function you use $state.go(‘view’)


#7

I`m using the last version but this code did not work.


#8

Can you help me???


#9

Check out this codepen: http://codepen.io/anon/pen/emWOoQ

It is using the latest beta (beta 14) and it is calling the function goBack() which I defined in the back-button. If you open the console you will see it logs “going back” and it is always redirecting to the contact page. You would have to modify this code to go to the state you want and only affect a specific state.


#10

Thanks.
clearHistory does not seem to work any more on beta 14 (I did move to $ionicHistory).
The historyRoot option does the trick.


Beta 14 $ionicHistory.clearHistory() not working on Android
#11

“deprecated” must mean “still working until removed”, must not it?.. Have you broken $ionicViewService with marking it as “deprecated”? Well, it can be excused only in beta…


#12

I’m not sure what you’re saying. I am just reiterating where it was stated as deprecated in this post. If it is still working that is something the Ionic team would have to address.


#13

Oh, I’ve read a comment from @thiago as ‘it stopped working’ and have been disappointed. Sorry to blame you. Our app is still $ionicViewService dependant, and we are not going to switch to $ionicHistory util view service is removed completely. So, it’s quite sensitive for me.


#14

Thanks @brandyshea! after trying different variation of $ionicHistory.clearHistory(); and $ionicHistory.clearCache(); on enter and on leave, your solution is the only one that worked for me