Restoring Transient State
When an app transitions from being tombstoned or dormant back to the running state, the PhoneApplicationService.Activated
event is raised. This provides an opportunity to restore the transient and persistent state of the app.
Note
Restoring the transient state of a PhoneApplicationPage
should be performed in its OnNavigatedTo
method.
Restoring the transient state involves taking the user to the point where she was when the Deactivated
event occurred, and may involve restoring the positions of UI elements,
repopulating viewmodel properties, and so on. The goal is to provide
the user with a seamless experience, and to emulate a multiple
application environment, in which the application appears as though it
was left running in the background.
The following code demonstrates handling of the PhoneApplicationService.Activated
event, to restore transient and persistent state:
void Application_Activated(object sender, ActivatedEventArgs e)
{
/* Restore persistent state like so: */
someObject = IsolatedStorageSettings.ApplicationSettings["Key"];
/* Restore transient state like so: */
DataContract = PhoneApplicationService.Current.State["DataContractKey"];
}
Saving Persistent State
Persistent state is usually stored whenever
transient state is stored. In addition, your app should save its
persistent state when it is closing, by subscription to the PhoneApplicationService.Closing
event. Persistent state may include files or application settings, as shown in the following excerpt from the App
class:
void Application_Closing(object sender, ClosingEventArgs e)
{
System.IO.IsolatedStorage
.IsolatedStorageSettings.ApplicationSettings["someObject Key"]
= someObject;
}
Note
Transient state should not be retained when the Closing
event occurs.
Note
Windows Phone 8 offers an alternative storage API present in the Windows.Storage
namespace.