Windows Phone Developer Tip: Navigation Events and Tombstoning

computer-geek-gets-fitting-tombstone_1One of the developer features that I think all Windows Phone developers should be supporting is tombstoning.  If you are writing Windows Phone apps and are not supporting Tombstoning then please do just makes UX better which makes happy customers!  If you have not implemented tombstoning in your app, make sure you you read the Windows Phone Execution Model which has details on this.

So what is the tip?  First some background to give some context.  We built a picture viewer for one of our apps, and to conserve memory on the Page.OnNavigateFrom we dispose of a bunch of objects in the view model as we don’t want those lingering around.  Our primary concern was WriteableBitmaps.

The issue was we were cleaning up objects in the Page.OnNavigateFrom method and cleared our objects before we had a chance to save any state in the PhoneApplicationService.State.  This is because Page.OnNavigateFrom was called before the PhoneApplicationService.Deactived event was fired.  So to work around this we did the following

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    if (!e.Uri.ToString().Equals("app://external/"))
    {
        //Clean up only if not tombstoning
    }
    base.OnNavigatedFrom(e);
}

This way our PhoneApplicationService.Deactived event handler can save any state required.

The key take away is the Uri your app navigates to when being tombstoned and that is “app://external/”  This is the Uri you need to look for to see if you are being tombstoned before cleaning up any view models or objects.

Again, this is relevant in the app we where building and the code above may not fit your scenario exactly.  But the “app://external/” Uri may be useful at some point when developing for Windows Phone!  And you get this Uri if you are using Windows Phone Mango or Nodo.


Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c07/h02/mnt/110221/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405

Leave a Reply