Saving Transient State

,

The Deactivated event provides an application with the opportunity to save its transient and persistent state.


Note

Saving the transient state of a PhoneApplicationPage should be performed in its OnNavigatedFrom method, as you see later in the chapter.


The goal is to enable restoration of the application to its prior state before being tombstoned. It should be assumed, however, that when the Deactivated event occurs, that the application is going to be closed, moving to the closed state. The user may, after all, opt not to resume the application, or may use the Start Experience to re-launch the application, rather than using the hardware Back button to return to the application. Moreover, if the user launches many other apps, your app may get bumped off the end of the Back button application stack.

The Visual Studio new project templates place an empty handler for the Deactivated event in the App class. See the following excerpt:

void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    /* Save transient state like so:
     * PhoneApplicationService.Current.State["DataContractKey"]
     *        = DataContract;
     */

    /* Save persistent state like so:
     * IsolatedStorageSettings.ApplicationSettings["Key"] = someObject; */
}

You can also subscribe to the Deactivated event elsewhere, in the following manner:

PhoneApplicationService.Current.Deactivated += OnDeactivated;

void OnDeactivated(object o, DeactivatedEventArgs args)
{
//...
}


Caution

The operating system gives an app 10 seconds when the PhoneApplicationService.Closing event occurs, before it is forcibly terminated. If the time required to save your app’s state exceeds this amount, its state should be saved periodically, and perhaps incrementally, while it is running.



Note

The Windows Phone emulator terminates an application if it takes longer than 10 seconds to display its first visual. Therefore, when debugging an Activated or Launched event, if this time is exceeded the application exits and the debugger detaches before any UI elements can be shown.


Transient State Requirements

All objects to be stored in the PhoneApplicationService.State property must meet one of the following requirements:

Image It is a primitive type.

Image It is a known serializable reference type including decimal, string, or DateTime, with a matching System.Convert.ToString method signature.

Image It is capable of being serialized using a DataContractSerializer. To achieve this, it must be decorated with a System.Runtime.Serialization.DataContract attribute. Each property or field intended for serialization must be decorated with the DataMember attribute, and in turn each serializable property or field type must be decorated with the DataContract attribute.

Storing an application’s transient state can be difficult because objects containing state often have event subscriptions to or from other objects that are not serialized. Also, types from third-party libraries are usually not decorated with the DataContract attribute, preventing serialization.

To avoid difficulties in serializing objects, keep classes that you intend to serialize as simple as possible, without dependencies on third-party classes. A design pattern that is commonly used for this situation is the Memento Pattern. For more information, see http://en.wikipedia.org/wiki/Memento_pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.121.54