Unlike a traditional operating system on a desktop or server computer, Windows Phone 7 does not run multiple applications concurrently. Only one application—the foreground application—can be active and running at any one time. When the user switches away from the foreground application, or when an external event causes another application to become the foreground application, the original application is deactivated in a process referred to as “tombstoning.” When the user switches back to the application, it is reactivated.
This means that if you want to preserve state (including page and control state) when your application stops being the foreground application, you must handle the events exposed by the operating system to save any state you require to implement a seamless resume experience (such as setting the contents of text boxes, a selected date, and so on). This saved state will be used to restore the application and page state when the application reactivates.
External events that may cause the application to be tombstoned include events such as a phone call being received; a system dialog box, such as the built-in photo selector, being displayed; or the phone lock screen being activated.
In addition, an application that is tombstoned may be completely removed from memory if, for example, the operating system detects that the device is running low on resources. In this scenario, when the user goes back to the application, it will restart. Therefore, you must make sure that you have preserved all persistent state to storage whenever your application is deactivated.
You handle the Launching event to retrieve state for your application from isolated storage on the device and/or from a remote service, as appropriate, when the user first starts your application from the phone’s Start screen. You also handle the Closing event that occurs when the user closes your application (or presses the Back button to navigate backward through the pages of the application past the application’s first page) to save any state your application requires to isolated storage on the device and/or to a remote service, as appropriate.
You must also handle the Deactivated and Activated events. The Deactivated event occurs before the application is tombstoned, and you should save any transient state to the phone application service so that it can be quickly reinstated when the application is resumed. The operating system provides two objects where you save that transient state. Save transient application data to the PhoneApplication Service.State property, and save transient page state to the PhoneApplicationPage.State property. There is a time limit for the Deactivated event to complete. The device may terminate the application if it takes longer than 10 seconds to save the transient state.
Note: There is no guarantee that an application that was deactivated will be reactivated, so, in addition to the transient state, you should also save any persistent state to isolated storage or a remote service, as appropriate. Applications that require a large volume of persisted data may need to save that data incrementally while the application is running to ensure timely deactivation.
The Activated event occurs when the user navigates back to your application using the Back button, or when a launcher or chooser that has interrupted execution to handle a phone call or to display the lock screen is deactivated. When your application is reactivated, you should retrieve the transient data from the PhoneApplication Service.State and PhoneApplicationPage.State property bags in the Activated event. As with the Deactivated event, there is a time limit
for the Activated event to complete. The device may terminate the application if it takes longer than 10 seconds to reload the transient state and return the application to a running state.
Note: Launchers and choosers allow other applications or operating system features to execute while your application is running. For example, the PhoneCallTask launcher activates when a phone call is received, or your application may use a launcher, such as the PhotoChooserTask, to select a photo from the device storage. For more information, see “Launchers and Choosers for Windows Phone” on MSDN (http://msdn.microsoft.com/en-us/library/ff769556(VS.92).aspx).
Figure 2 shows the life cycle, execution flow, and events you must handle to preserve state in a Windows Phone 7 application.
For a full explanation of the ways that you should handle the life cycle events in your application, see “Application Lifecycle” on Channel 9 (http://channel9.msdn.com/learn/courses/WP7Training-Kit/WP7Silverlight/ApplicationLifetimeWP7Lab/) and “Execution Model for Windows Phone” on MSDN (http://msdn.microsoft.com/
en-us/library/ff769557(VS.92).aspx). A useful resource that explains how to use the PhoneApplicationService class is “How to: Preserve and Restore Page State for Windows Phone” on MSDN (http://msdn.microsoft.com/en-us/library/ff967548(VS.92).aspx).