Windows Phone Application Life Cycle

The Windows Phone execution model introduced in the beta release of the developer tools defines how a Windows Phone application is expected to behave during its life cycle. This model exposes four events in the PhoneApplicationService class that an application must listen to:

  • Launching is fired when the application is launching, for instance from the Start screen. It is not fired when the application activated.

  • Activated is fired when the application was in background and is brought to foreground, for instance when coming back from the web browser previously launched with WebBrowserTask.Show(). It is not fired when the application is launching, for instance from the Start screen.

  • Deactivated is fired when the application is sent to background, for instance when the Start key is pressed or when the web browser is launched with WebBrowserTask.Show(). It is not fired when the application is closing.

  • Closing is fired when the application is closing, for instance when the Back key is pressed while in the application’s main page. It is not fired when the application is deactivated.

Once it has been deactivated, an application is said to be “tombstoned”: it is not alive anymore and must have saved any transient data that it would like to restore in the event it is activated later on. Transient data include everything that is relevant only to the current instance of the application, such as a value entered in a TextBox or data returned by a web service query. It is different from persistent data which are relevant to all instances of the application and saved to isolated storage, such as account info and user settings.

It is the responsibility of the application to save its transient data using the PhoneApplicationService.State and PhoneApplicationPage.State properties when the Deactivated event is fired.* These properties reference Dictionary objects that will be restored by the framework if the same instance of the application is activated later on; they will be discarded if a new instance of the application is launched. Note that an application has ten seconds to handle the Deactivated event, after which it will be killed by the framework.

* More precisely, an application should save page-level transient data to the PhoneApplicationPage.State property when the page’s OnNavigatedFrom method is called. It should only have to save its instance-level transient data to PhoneApplicationService.State when the Deactivated event is fired. Check the Execution Model Best Practices topic on MSDN for a helpful list of do’s and don’ts related to tombstoning.







Fixing MethodAccessExceptions when deserializing an XML document

If a call to XmlSerializer.Deserialize() ends up throwing a MethodAccessException in your Windows Phone application, ensure that each object being deserialized follows the rules below:

  • The type of the object being deserialized has a public parameterless constructor.
  • The fields and properties of the deserialized type that represent XML elements all have public get and set accessors.

This applies to the type specified to the XmlSerializer constructor, and recursively to the type of each field and property of the deserialized type that represents an XML element.