Windows Phone 7 Quick Tutorials: Part 4 – Tombstoning and Data Persistence

In the previous articles of the series we’ve seen how to create simple WP7 Silverlight applications, use the app bar and navigate between different pages. In this article we’ll tackle the application lifecycle and how can we store the application’s settings.


On Windows Phone 7 only one application can run at a time, as the system does not support multi-tasking. The reason is that multiple applications running on the background can degrade the performance. On the other hand, users can navigate forward or backwards through the stack of (previously) running applications; forward, by starting applications from the installed applications list or from a tile on Start, and backwards using the Back button.

In addition, applications can be interrupted by Launchers (APIs that WP applications can use to enable tasks such as making phone calls or sending emails, without returning any data to the calling application) or Choosers (APIs that WP applications can use to launch built-in application that allows the user to complete a task, such as taking a picture, which returns some data to the calling application).

When the user presses the Back button, the application is terminated. However, when the user presses the Start button, or a launcher or chooser starts, the application is not terminated, but put into a sort of hibernation, with the system maintaining application state; if the user returns to the application by pressing the Back button, or when the launcher or chooser terminates, the application is resurrected and the application state restored. This process is called tombstoning.

The image below shows (in green) the possible states of the application and the events fired when the state changes (in red).

State and Events

Applications usually contain data that should be preserved when deactivating or closing and restored when activated or re-started. This can be either at page level or application level:

  • Page state: is the state specific to a particular page, usually referring to the visual appearance (content of controls, position of scroll bars, etc.).
  • Application state: is the state common to the entire application and not specific to a particular page.

Page state should be managed in the following Page event handlers:

  • OnNavigatedTo: called when a page becomes the active page in a frame
  • OnNavigatedFrom: called when a page is no longer the active page in a frame

Application state should be managed in the handlers for the events exposed by the PhoneApplicationService (shown in red rectangles in the previous image):

  • Launching: occurs when the application is being launched.
  • Closing: occurs when the application is terminated as a result of pressing the Back button on the first page.
  • Activated: occurs when the application is made active after being previously tombstoned, as a result of returning to the application with the Back button or a Launcher/Chooser finishing work.
  • Deactivated: occurs when the application is tombstoned as a result of pressing the Start button or a Launcher/Chooser activating.

Preserving State

The data associated with your application can be grouped in two categories:

  • Transient: data that is reinitialized each time the application starts, but needs to be preserved when the application is tombstoned
  • Persistent: data that must be maintained across different runs; it must be saved and restored both when the application is tombstoned and completed terminated.

The framework offers two different storages for state persistence, both of them being dictionaries:

  • PhoneApplicationService.Current.State: used for transient data only; this dictionary is automatically persisted when the application is tombstoned and restored when the application is activated again, but is not saved when the application is closing. It can be only used to store data that is not required when a new instance of the application starts.
  • IsolatedStorageSettings.ApplicationSettings: used for persistent data, required between different runs of the application. Can be used both when the application is just tombstoned or completely terminated. They are stored in the isolated storage, which is an area of the disk storage that each WP7 application gets access to.

In addition to these dictionaries you can use other means to save your data, such as the types available in the System.IO.IsolatedStorage namespace: IsolatedStorageFile and IsolatedStorageFileStream. IsolatedStorageSettings is a class designed to ease access to the isolated storage. This is what we’ll use in this article.

More by Author

Must Read