How does ionic history work and when is a non-root stack created?

I’ll try to answer this question even if you might not find all the info you’re asking.

Lot of people – included myself – seem to struggle to understand how the navigation system and the history works.

I’ve answered a question earlier trying to explain why things don’t work as expected.
It seems the navigation keeps track of each view the user has visited using a collection.
Actually there are 2 collections in the $ionicHistory object.
The first one $ionicHistory.viewHistory().views seems to keep track of each visited view in the current stack while the other $ionicHistory.viewHistory().histories keeps track of all the histories for the whole app.

You might have different types of history for tabs, sidemenus or regular views.

You can see how parallel independent histories work in this codepen.
There are 2 different histories there. One is for the home tab and the second is for the about tab.

Navigating through the sub-items in each tab and going back to the previous tab you’ll notice that the navigation system has remember the previous state.

I’ve prepared another plunker here where you can see how the navigation works with some details displayed in the page.

The collection of views $ionicHistory.viewHistory().views is updated each time the user visits a new page (the current view in the collection is wrapped in square brackets).

If the view has been added to the collection it won’t (it shouldn’t) be added again.

You can change the behaviour clearing the history ($ionicHistory.clearHistory()) or setting the root for the current history:

    historyRoot: true

In the Invoice page of my plunker there’s a green button (Other Root View). When pressed I set the new history root and change state:

    historyRoot: true

Things work as expected and in fact now I don’t have a back view and my stack contains only the current view.

Things get messed up when you try the sequence:

Home - Contacts - Invoices - Home (button in the header).

Now it’s seems Ionic has lost control of the sequence and keeps on adding views to the collection.

Pressing the home button should clear the back button, since we are on the root for the current history, but it doesn’t happen.

Using the same pattern over and over increases the size of the collection indefinitely.

I guess this is not the right behaviour and a fix is needed.

Going back to your question.

The back button in Android works … meaning the it follows the same pattern.

Modals luckily are not treated as regular views and don’t affect the collection.

Leave a Comment