Wednesday, December 8, 2010

Tomb stoning on the Win7 Mobile Platform

grave_thumbTomb stoning is  the process of saving the data and state of your application when it’s terminated such that it can resume where it left off when it is started back up again. There are many cases where you application may be suddenly terminated on the phone. Examples include incoming phone calls or activation of the camera on the device. Tomb stoning is an essential feature that almost every application should support to avoid customer frustration with your application.

Tomb stoning an application is fairly straightforward. Every application on the phone has its own permanent storage space on the device that is referred to as isolated storage.

Classes for isolated storage can be found in under the System.IO.IsolatedStorage namespace.

You can use these classes to store and reload the state and data for your application.

In your App.xaml file, the events you should monitor for are the following:

  • Application_Launching – Code to execute when the application is launching. This code will not execute when the application is reactivated
  • Application_Closing – Code to execute when the application is closing (i.e. user hits the back button). This code will not execute when the application is deactivated
  • Application_Activated  – Code to execute when the application is activated (brought to foreground). This code will not execute when the application is first launched
  • Application_Deactivated – Code to execute when the application is deactivated (sent to background. This code will not execute when the application is closing

These should all be there by default when you create a new Silverlight mobile project:

<shell:PhoneApplicationService 
 
    Launching="Application_Launching" 
 
    Closing="Application_Closing" 
 
    Activated="Application_Activated" 
 
    Deactivated="Application_Deactivated"/>
 
 
 
private void Application_Launching(object sender, LaunchingEventArgs e)
 
{
 
    LoadApplicationState();
 
}
 
 
 
private void Application_Activated(object sender, ActivatedEventArgs e)
 
{
 
    LoadApplicationState();
 
}
 
 
 
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
 
{
 
    SaveApplicationState();
 
}
 
 
 
private void Application_Closing(object sender, ClosingEventArgs e)
 
{
 
    SaveApplicationState();
 
}
 



Typically data I want stored I keep as static variables so they are easily accessible from your Appl.xaml.cs class. In the example below, I am loading and retrieving the “level” your application is on.


private void LoadApplicationState()
 
{
 
    int currentLevel = 1;
 
 
 
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 
    settings.TryGetValue<int> ("level", out currentLevel);
 
 
 
    Consts.CurrentLevel = currentLevel;
 
}
 
 
 
private void SaveApplicationState()
 
{
 
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
 
 
 
    settings["level"] = Consts.CurrentLevel;
 
}
 



Thanks,


Rizwan Suddle