WP7 getting started (3) – Lifecycle management

This is the third in a series of blog posts describing how to get started with Windows Phone 7 development. It is written based on my own experience getting started with Windows Phone 7 development.

The previous entry, where I created a simple app for collecting feedback on how a presentation went is available here.

Application life cycle

An obvious problem with the app created in the previous post is that it reset the counters every time the app is started.

This means that the counters are reset if the phone receives an incoming call, if it goes to sleep or if somebody accidentally presses the Start button. This is of course very inconvenient.

To address this problem, we need to look at application life cycle management. Here is a good throrough description.

The issue at hand is called tombstoning. Since Windows Phone doesn’t allow apps to run in the background, our application process gets killed, when the application doesn’t occupy the UI.

The user can then either start a new instance of the application (in which case the counters SHOULD be reset) or navigate “back” into the application (in which case the counters should NOT be reset).

So to resume at the point, where we left off, we have to save our state information, so it can be reloaded if the application gets reactivated.

There are two ways to save information, application state and page state.

Application state

Application state is the state of the application that is not related to a specific page in the application. It could e.g. be login information.

The application has a set of events that can be used to handle/save/restore application state:

Page state

Page state is the state of a single page, typically UI-related such as content of textboxes or scroll-position of a scroll view.

These methods are called every time a page is navigated to or from, so if the application gets deactivated, you can store page state information in OnNavigatedFrom and restore it in OnNavigatedTo.

 Using Page state to save counters

In this case the counters are page-specific, so they will be treated as page-state information for now.

The following code will save the counter values during deactivation and restore them during reactivation. You might notice that there is room for some improvement here, if one were to refactor the code a bit, but I choose not to in order to keep the example as simple and illustrative as possible.

When you test the application, be sure to distinguish between wether the app is deactivated or closed and wether it is reactivated or launched.

An app is closed, when the user backs out from the main screen with the back button. Otherwise it is deactivated (tombstoned).

An app is launched, when it is started from the start screen (regardless of wether it was previously deactivated), otherwise it is activated. The State dictionary is empty, when an application is launched.


This Post Has One Comment

Leave a Reply

Close Menu