Saturday, December 22, 2012

Clearing the back stack in Windows Phone

With the new Windows Phone 8 Fast Resume feature (so cool!), I had a problem when launching the app from a secondary Live Tile.  Since the tile had a deep link, it would resume the app and open the deep link.  Sounds fine, but if the user then clicked the Back button, it wouldn't exit (as expected).  Rather, it would go back to where they were the last time they were viewing the app!  It turns out the quick fix is to run through the back stack items in NavigationService.  Of course, simply calling GoBack() is a bad idea since that would mess cause a flicker at the very least, so instead notice the new method: RemoveBackEntry().  This clears the last item from the back stack without showing anything!  A simple while loop can now remove all the back entries:

while( NavigationService.CanGoBack ) NavigationService.RemoveBackEntry();