Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WP7 TombstoneHelper

0.00/5 (No votes)
12 May 2011 1  
When your application gets tombstoned, you get an opportunity to save some state about your application. What should be saved, where to save it, and when?

“The procedure in which the Operating System terminates an application’s process when the user navigates away from the application. The Operating System maintains state information about the application. If the user navigates back to the application, the Operating System restarts the application process and passes the state data back to the application.”

When your application gets tombstoned, you get an opportunity to save some state about your application… Common questions I get is what should I save, where should I save it, and when? Let me try and address some of these questions:

What Should I Save

  • Panorama: Panorama.SelectedIndex
  • Pivot: Pivot.SelectedIndex
  • ListBox: ScrollViewer.VerticalOffset and ScrollViewer.HorizontalOffset
  • TextBox: TextBox.Text, TextBox.SelectionStart, and TextBox.SelectionLength
  • CheckBox: CheckBox.IsChecked
  • RadioButton: RadioButton.IsChecked
  • Slider: Slider.Value

Where Should I Save It

Each page has a State dictionary and there is also an application-wide dictionary as part of the PhoneApplicationService. These state dictionaries get automatically persisted when tombstoned! The helper class will save each control that you give to it on the page specific dictionary, and it will use the control's name as the key!

When Should I Save It

The last question is when? You have two options here… Application lifecycle events (Application_Activated and Application_Deactivated), or on a page level you can override OnNavigateTo and OnNavigateFrom!

Since the helper class really tries and addresses page specific state, it makes sense that we use OnNavigateTo/OnNavigateFrom!

Let's Try It Out…

The helpers are implemented as extension methods so all you have to do is:

public partial class NormalPage : PhoneApplicationPage
{
    public NormalPage()
    {
        InitializeComponent();            
    }

    protected override void OnNavigatedFrom(
              System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        this.Save(textBox);
        this.Save(checkBox);
        this.Save(radioButton1);
        this.Save(radioButton2);
        this.Save(slider);
    }

    protected override void OnNavigatedTo(
              System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        this.Load(textBox);
        this.Load(checkBox);
        this.Load(radioButton1);
        this.Load(radioButton2);
        this.Load(slider);
    }
}

Easy, isn’t it!

Here is the latest version of the sample application.

Read more about the Windows Phone execution model here:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here