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

WP7 TombstoneHelper

, 29 Mar 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
What data to save when your application is tombstoned.

“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 are 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 their 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 will be using 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 sample application.

Read more about the Windows Phone execution model here:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

rudigrobler

South Africa South Africa
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMr.Jinky11-Aug-11 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 29 Mar 2011
Article Copyright 2011 by rudigrobler
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid