Click here to Skip to main content
15,885,953 members
Articles / Mobile Apps / Windows Phone 7

Managing Tombstone State in a Windows Phone 7 Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 May 2011Ms-PL2 min read 15.6K   4  
How to manage Tombstone State in a Windows Phone 7 Application

Before we continue, I trust that you know exactly the meaning of tombstone state in a Windows Phone 7 application. If this is not the case, simply read Yochay’s great posts on the application execution model (Part 1, Part 2, Part 3).

Every Windows Phone 7 application needs to manage tombstoning correctly to provide a better user experience and pass the application certification requirements.

When your application goes into tombstone state, it is your responsibility to save all the application and page state, so that when the page gets reloaded, it can recover properly and reload the previous data. This way, the user doesn’t even realize that the application process got terminated and reloaded.

To ease the handling of the tombstone state, I’ve created a simple StateManager class which helps in serializing the data when going into tombstone state and deserializing the data when you get back.

How to Use StateManager?

Suppose your current page has the following data members you want to save across tombstoning:

  • LastPosts
  • Posts
  • Comments
  • Images

Now all you need to do is to save them when the application goes into tombstone state and reload them when it gets back from tombstone state.

Saving the Data

The proper place for the code that saves the data resides in the OnNavigatedFrom method. In this method, you should call the helper extension method SaveState for each member of the page that you want to save, passing the value and the key on which to save it:

C#
protected override void OnNavigatedFrom(NavigationEventArgs e)
 {
     this.SaveState("LastPostsKey", LastPosts);
     this.SaveState("PostsKey", Posts);
     this.SaveState("CommentsKey", Comments);
     this.SaveState("ImagesKey", Images);
 }

Loading the Data

The proper place for the code that loads the data when returning from tombstone state resides in OnNavigatedTo method. In this method, you should call the helper extension method LoadState for each member of the page that you want to load, passing the key related to the member’s value:

C#
protected override void OnNavigatedTo(NavigationEventArgs e)
 {
     LastPosts = this.LoadState<ObservableCollection<RssItem>>("LastPostsKey");
     Posts = this.LoadState<ObservableCollection<RssItem>>("PostsKey");
     Comments = this.LoadState<ObservableCollection<RssItem>>("CommentsKey");
     Images = this.LoadState<ObservableCollection<ImageItem>>("ImagesKey");
 }

Definition of StateManager

So how is the StateManager class defined? It is as simple as the following two extension methods:

C#
/// <summary>
 /// State Manager
 /// </summary>
 public static class StateManager
 {
     /// <summary>
     /// Saves a key-value pair into the state object
     /// </summary>
     /// <param name="phoneApplicationPage">The phone application page.</param>
     /// <param name="key">The key.</param>
     /// <param name="value">The value.</param>
     public static void SaveState(this PhoneApplicationPage phoneApplicationPage, 
                                  string key, object value)
     {
         if (phoneApplicationPage.State.ContainsKey(key))
         {
             phoneApplicationPage.State.Remove(key);
         }
 
         phoneApplicationPage.State.Add(key, value);
     }
 
     /// <summary>
     /// Loads value from the state object, according to the key.
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="phoneApplicationPage">The phone application page.</param>
     /// <param name="key">The key.</param>
     /// <returns>The loaded value</returns>
     public static T LoadState<T>(this PhoneApplicationPage phoneApplicationPage, string key)
         where T : class
     {
         if (phoneApplicationPage.State.ContainsKey(key))
         {
             return (T)phoneApplicationPage.State[key];
         }
 
         return default(T);
     }
 }

Note: This code was first published as part of the “Using Pivot and Panorama Controls” lab found in the Windows Phone Training Kit for Developers, which I wrote for Microsoft.

That’s it for now,
Arik Poznanski

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
-- There are no messages in this forum --