Windows Phone Mango – Tombstoning and life cycle

Before Mango the Windows Phone application could be in four states, Running, Deactivate, Tombstoned and Activate. In Mango there is a new state called Dormant. This extra state is there to provide us with fast app switching. It is important for the developer to understand the Windows Phone application life cycle and to understand what actions the developer needs to take at each state and event to provide an application that is responsive and provides a consistent experience.

The following overview explains the life cycle of Windows Phone applications and all the actions the developer should take and be aware of.

Windows Phone Life Cycle - Image taken from MSDN

Launching Event

The Launching event is raised when a new instance of the application is launched by the user. At this stage the user expect a new fresh instance of the application and quickly wants to start using it. To provide this experience you should never put code in this event handler that takes time, really avoid all resource heavy tasks and perform them when the application is loaded and perform these resource intensive tasks on a background thread.

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
}

Running State

Once the application is launched it is in the running state. A Windows Phone application will continue to run until the user navigates away from it or if the lock screen engages. As a developer you should never provide the user with a button to exit or quit the application. This will break the consistent user experience used in Windows Phone and Metro. The user should be familiar with that pressing the back button when you are at the first page for an application exists the application.

OnNavigatedTo Method

The OnNavigatedTo method is called when the application is first launched, when the user navigates between pages in your application, and when the application is relaunched after being dormant or tombstoned.

At this point the developer needs to check if the page is a new instance or not, if it is not then no action is needed. If it is a new instance, data in the state dictionary needs to be restored for the page.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
/// Possible NavigationModes
/// NavigationMode.New
/// NavigationMode.Back
/// NavigationMode.Forward
/// NavigationMode.Refresh
if (e.NavigationMode == NavigationMode.New)
{
}

base.OnNavigatedTo(e);
}

OnNavigatedFrom Method

Whenever a user navigates away from a page, the OnNavigatedFrom method is called. This is called if the user navigates forward, launching another application, or launching a chooser. At this stage you have to as a developer store the page state so that it can be restored if the user navigates back to it. Note that when navigating backward you do not need to save page state since the whole page will be re-created the next time it is visited.

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// Stop timers, turn of XNA rendering etc..

base.OnNavigatedFrom(e);
}

Deactivated Event

When the user navigates away from your application the Deactivated event is raised. In this event handler the developer needs to store transient application state in the State dictionary so that once reactivated an application can use the values stored in State dictionary to transient application state.

Note that once an application is deactived it could also be terminated and its State dictionary is not preserved. As a developer you should also store any state information/data that should be persisted across application instances to isolated storage during the Deactivated event.

// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
}

Dormant State

A new state that is introduced in Mango to provide fast app switching. Pre-Mango the application was tombstoned when the deactivated event was raised, now in Mango the application will be put into a dormant state. In this state all of the application threads are stopped and the application remains intact in memory. Since the application is kept in memory and preserved, no actions are needed when the application is reactivated.

Note, that if new applications are started and these applications need more memory than is available, the operating system will start tombstoning applications. It will start with the application that is “oldest”. A developer should never assume that the application stays in a dormant state, if application data needs to be restored it should be saved in isolated storage at Deactivated event.

Tombstoned State

A tombstoned application has been terminated although information about its navigate state and history, and state dictionaries populated during Deactivated are preserved. At time writing the device will only store information for up to five applications at a time, if more applications are tombstoned the application will simply terminate.

Activated Event

When the application returns from a dormant or tombstoned state the Activated event is called. The IsApplicationInstancePreserved property should be checked to determine if the application is returning from dormant or tombstoned state. Since the state is preserved in a dormant state by the operating system, nothing has to be done, if activated from a tombstoned state then the developer needs to use the State dictionary to restore application state. Since this event is thrown when the application “starts” the developer should not put resource heavy operations here that halts the user experience. Instead do all resource intensive tasks once the application is loaded and on a background thread.

// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (e.IsApplicationInstancePreserved == true)
{
// Application was dormant, do nothing
}
else
{
// Load data as normal
}
}

Closing Event

When the user navigates backwards past the first page of an application it will exit and the Closing event is raised. At this stage you should not store any data in State dictionary since that will not be loaded once the application is restarted. Instead store any data that should be persisted across application instances in isolated storage.

Note: The operating system has a limit of 10 seconds to complete all application and page navigation events when closing. If that time limit is exceeded, then the application will simply be terminated. So as a developer you should have as a rule to save persistent state throughout the application life cycle to avoid this unwanted termination and possible loss of data.

// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
}

Summary

Following is a summary of all the states, methods and events you need to know about and handle properly when building applications for the Windows Phone

  • Launching Event – Execute very little code, no resource heavy tasks
  • OnNavigatedTo – Check if it is a new instance. If not check if there is data in State and restore it
  • OnNavigatedFrom – If not a backward navigation, save information to the State Dictionary
  • Deactivated Event – Save application state to State dictionary. Also save persistent state to isolated storage that needs to be restored between application instances in case of application termination.
  • Activated Event – Check IsApplicationInstancePreserved property. If true (application was dormanted) do nothing. If false, use State dictionary to restore application state.
  • Closing Event – Save persistent application data to isolated storage

Happy Coding!

Advertisements
  1. August 29th, 2011

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: