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

WP7 TombstoneHelper

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Mar 2011CPOL2 min read 9.9K   3   1
What data to save when your application is tombstoned

Image 1

“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 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 will be using OnNavigateTo/OnNavigateFrom!

Let's Try It Out…

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

C#
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:

This article was originally posted at http://www.rudigrobler.net:80/blog/wp7-tombstonehelper

License

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


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Mr.Jinky11-Aug-11 3:08
Mr.Jinky11-Aug-11 3:08 

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

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