Prior to Windows Phone 8, the only way to
programmatically exit a XAML-based app was to raise and not handle an
exception. It was an inelegant hack for a not-uncommon scenario.
In Windows Phone 8, however, the API includes a direct mechanism for terminating an app. It is achieved using the Application
class’s Terminate
method, as demonstrated:
Caution
The Terminate
method does not raise any application life cycle events. Before you call this method, you must save the state of the app.
4. Saving Transient State
The Deactivated
event provides an application with the opportunity to save its transient and persistent state.
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:
- It is a primitive type.
- It is a known serializable reference type including decimal
, string
, or DateTime
, with a matching System.Convert.ToString
method signature.
- 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.