The Windows Phone operating system allows only one application to run at a time. When the user navigates away from the application, the operating system terminates the application. To provide a seamless experience to the user, the operating system provides support to help you restore your application to a presentable state when it's reactivated. This QuickStart describes the tombstoning process, the application lifecycle, and how to restore state.
This QuickStart contains the following sections:
The Windows Phone operating system doesn't allow any third-party applications to run in the background; therefore, only one application can run at a time. The reasons for not allowing any third-party applications to run in the background are to preserve battery life and to ensure a consistent and responsive user experience. When the user navigates away from the application, the Windows Phone operating system deactivates the application. This is called tombstoning. It's the process in which the operating system deactivates an application's process when the user navigates away from the application. An application can get deactivated in the following cases:
When the tombstoned application is activated again, it must look the same as it looked before tombstoning in order to maintain a seamless user experience. Ideally, the user shouldn't know that it's a completely new process when he reactivates a tombstoned application. For example, suppose you open an application with a long page, and you've scrolled down a bit to read the page, then you navigate to another page. When you go back to the original page, it's not too upsetting if you've lost your place and you're back at the top of the page. On the other hand, if you've just spent minutes filling out a large form, you definitely don't want to see all your work gone after another page tells you that you've made one tiny error.
To achieve this, when the application gets deactivated, the operating system maintains some state information about the application in memory and uses that information to restore your application to a presentable state when it's reactivated.
Windows Phone provides methods and events to store and restore application and page states. These methods and events are described later in this QuickStart.
Since Window Phone applications can be tombstoned, they have a unique life cycle that you need to understand. The operating system provides four events that indicate the state of your application in the life cycle. You can use these events to take appropriate action in your application. These events are Launching, Closing, Deactivated, and Activated. The following illustration shows when these events occur.
When an application is started from the Start screen, it is said to be launched. Whenever the user launches an application, a new instance of the application is created. When the application is started, a Launching event is raised. The following code shows the Launching event that is present in App.xaml.cs file.
You can use the Launching event to incrementally save settings and other persisted data to reduce the amount of data that needs to be saved when the application's state changes. This is optional, and for applications with a small amount of persisted data, may be unnecessary.
By pressing the hardware Back button, the navigation model allows the user to "step back," or navigate backwards through the application pages, and even across applications. However, once the user reaches the application's first page, pressing the Back button again raises the Closing event and terminates your application. You can use this event to save persistent data to isolated storage. The following code shows the Closing event that is present in App.xaml.cs file.
An application could be deactivated (not closed) either as a result of another application taking control of the foreground, such as an incoming phone call, or as a result of the user pressing the Start button. In such cases, the Deactivated event is raised. The following code shows the Deactivated event that is present in App.xaml.cs file.
Unlike an application that is closed, a deactivated application may become tombstoned. This means that it is no longer running, but the operating system keeps a record of the application and stores a set of state data for the application. It's possible that the user will return to a tombstoned application, at which point it will be reactivated. So, you should store information about the application's current state by using the dictionary that is exposed through the PhoneApplicationService.State property in the Deactivated event handler. The data stored in this dictionary are transient state data, that is, data that will help the application reestablish the state of the application before it was deactivated. Because there is no guarantee that a tombstoned application will ever be reactivated, you should also save the application's persistent data to isolated storage during this event. All of the actions taken in the Deactivated event handler must be completed within 10 seconds or the operating system will terminate the application. So if your application has large quantities of persisted data, you may want to save incrementally as the application is running.
It's also possible for an application to be deactivated without being tombstoned. For example, if the user presses the Start button and then the Back button in rapid succession. For this reason, it's important not to do anything destructive in the Deactivated event handler. Your application should be able to continue after completing the Deactivated event handler without the Activated handler being called.
After an application has been deactivated and tombstoned, the user can return to the application by pressesing the Back button until the application is reached. When the user returns to a tombstoned application, it is reactivated and the Activated event is raised. You can use this event to read values from the PhoneApplicationService.State to retrieve the state of the application that the user experienced before the application was deactivated. The following code shows the Activated event that is present in App.xaml.cs file
Application state is the state of the application that isn't specific to a particular page. If your application uses global data such as login information or application settings like themes, it's likely that they'll be used by multiple pages within the application. This data is considered to be a part of application state. When your application gets reactivated after being tombstoned, the data are lost unless you specifically save the date. In general you use the Launching, Closing, Deactivated and Activated events to initialize and maintain application state. When your application is tombstoned, the operating system saves the contents of the dictionary that is exposed through PhoneApplicationService.State property. You can store the application-level state data to PhoneApplicationService.State property in the Deactivated event, and retrieve the data from PhoneApplicationService.State property in the Activated event. For more information see, How to: Preserve and Restore Application State for Windows Phone topic.
If your application has persistent data, you can store the data to isolated storage instead of using State dictionary. Isolated storage is a sandboxed file storage area on your phone's disk. It is analogous to the desktop file system. Data stored in isolated storage can take several seconds to load, so you should use it only for data that require potentially long-term storage and for non-serializable objects. Additionally, you should carefully consider where you put your isolated storage code in order to minimize the impact of any delays. For example, if you have a lot of application data to store, you should avoid saving it all at once upon application exit, and instead save it incrementally. In general, you should save data to isolated storage as soon as it becomes available, or as early as possible. For more information, see Isolated Storage QuickStart.
Page state is the visual state of a page in your application. When the user presses the Back button to return to the tombstoned application, the Windows Phone operating system will automatically display the last page in the application that the user was viewing. However, you're responsible for making sure that the page state is saved and restored so that it looks the same as it did when the user navigated away from it. Page state includes things such as contents of a text box and scroll position of a scroll viewer. This section will show you how you can save and restore the page state in order to provide a seamless experience for the user
To save and restore page UI state, you must use the page constructor, OnNavigatedTo method, and OnNavigatedFrom method. The page constructor is called on the first navigation to a page. OnNavigatedTo is called on every navigation to a page when the application and page have finished loading and are available for use. OnNavigatedFrom is called on every navigation away from a page when the application and page have not yet started unloading. You can initialize page-level information in the constructor, you can restore page state in the OnNavigatedTo method, and you can save page state in the OnNavigatedFrom method.
To save and restore page state, you typically perform the following steps.
The following example has two pages named home page and second page. When you click the Go to the Second Page button in the home page, the application navigates to the second page. The second page has a form with a text box, two radio buttons, and a check box. If you enter some data into these fields in the second page and then press the Start button to exit the application, the application goes into a tombstoned state. If you navigate back to the second page by using the Back button, you can see that the data that you entered before exiting the application are restored.
The following illustration shows the behavior if you don't include code to restore the page state.
Add the following code in the second page to save and restore the page state.
For more information on page state, see How to: Preserve and Restore Page State for Windows Phone.
The following are some execution model best practices that you can follow while developing a Windows Phone application:
For more information see, Execution Model Best Practices for Windows Phone.
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/