Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Managing Tombstone State in a Windows Phone 7 Application

, 2 May 2011 Ms-PL
Rate this:
Please Sign up or sign in to vote.
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 manages tombstoning

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 manages 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 was the user doesn’t even realizes 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 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:

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:

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:

/// <span class="code-SummaryComment"><summary>
</span> /// State Manager
 /// <span class="code-SummaryComment"></summary>
</span> public static class StateManager
 {
     /// <span class="code-SummaryComment"><summary>
</span>     /// Saves a key-value pair into the state object
     /// <span class="code-SummaryComment"></summary>
</span>     /// <span class="code-SummaryComment"><param name="phoneApplicationPage">The phone application page.</param>
</span>     /// <span class="code-SummaryComment"><param name="key">The key.</param>
</span>     /// <span class="code-SummaryComment"><param name="value">The value.</param>
</span>     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);
     }
 
     /// <span class="code-SummaryComment"><summary>
</span>     /// Loads value from the state object, according to the key.
     /// <span class="code-SummaryComment"></summary>
</span>     /// <span class="code-SummaryComment"><typeparam name="T"></typeparam>
</span>     /// <span class="code-SummaryComment"><param name="phoneApplicationPage">The phone application page.</param>
</span>     /// <span class="code-SummaryComment"><param name="key">The key.</param>
</span>     /// <span class="code-SummaryComment"><returns>The loaded value</returns>
</span>     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)

Share

About the Author

Arik Poznanski
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).
Follow on   Twitter   Google+

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.141029.1 | Last Updated 2 May 2011
Article Copyright 2011 by Arik Poznanski
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid