WP7 TOMBSTONING – State – Launching, Closing, Deactivated and Closing Events

 

Paused App

 

As you probably aware, WP7 only allows one application to run in the foreground at any given time  and no 3rd party applications are allowed to run in the background. Therefore when a user navigates away from your application, either to a chooser like picture chooser, or to a launcher like phone call, Windows Phone operating system terminates your application!

This process is known as “Tombstoning” and is the procedure in which the operating system terminates an application’s process when the user navigates away from the application. The operating system maintains state information about the application. If the user navigates back to the application, the operating system restarts the application process and passes the state data back to the application.

TIP: When you return from a tombstoned state the screen on the device/emulator goes black for a short while. At this point hit F5 in the debugger – the device is waiting for the debugger to attach. (Set the breakpoints beforehand, you have 10 seconds in which to reactivate the debugger).

There are 4 events related to Tombstoning- It is recommended that they are handled appropriately by developers within their own code (by default, the following stubs are created in App.XAML.cs code behind file:

// 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)
{
    System.Diagnostics.Debug.WriteLine("Application Launching");
}

// 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)
{
    System.Diagnostics.Debug.WriteLine("Application Activated");
}

// 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)
{
    System.Diagnostics.Debug.WriteLine("Application Deactivated");
}

// 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)
{
    System.Diagnostics.Debug.WriteLine("Application Closing");
}

 

LAUNCHING()

public event System.EventHandler<Microsoft.Phone.Shell.LaunchingEventArgs> Launching
    Member of Microsoft.Phone.Shell.PhoneApplicationService

Summary:

Occurs when the application is being launched. This happens when the user taps the icon for your application from the Start screen. Handle this event by loading persistent state from isolated storage and showing the first launch experience for the application. The application should not imply that it is resuming from a previous session.

CLOSING()

public event System.EventHandler<Microsoft.Phone.Shell.ClosingEventArgs> Closing
    Member of Microsoft.Phone.Shell.PhoneApplicationService

Summary:

 

Occurs when the application is exiting. This happens when the user uses the back key to navigate backwards past the first page of the application. Handle this event by saving persistent state, such as settings and user data.

 

DEACTIVATED()

public event System.EventHandler<Microsoft.Phone.Shell.DeactivatedEventArgs> Deactivated
    Member of Microsoft.Phone.Shell.PhoneApplicationService

Summary:

Occurs when the application is being suspended. This happens when the user navigates away from the application, either by starting another application or by invoking a launcher or chooser. Handle this event to preserve the application’s transient application state which you can restore in the handler for the Microsoft.Phone.Shell.PhoneApplicationService.Activated event if the application is reactivated. It is not guaranteed that the application will be reactivated. Therefore, it is a good practice to also save persistent state, such as settings and user data, in the handler for this event.

 

ACTIVATED()

public event System.EventHandler<Microsoft.Phone.Shell.ActivatedEventArgs> Activated
    Member of Microsoft.Phone.Shell.PhoneApplicationService

Summary:

Occurs when the application is being made active after previously being suspended. This can happen when the user navigates away from your application, either by starting another application or by invoking a launcher or chooser, and then returns to the application either by completing the chooser interaction or by using the back key to until the application is active again. Handle this event by loading persistent state from isolated storage and transient state from the State property. The application should present the user with an experience as if they are continuing a previous application session.

 

More:

http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2010/07/14/silverlight-for-windows-phone-7-tombstoning.aspx

http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/07/16/understanding-the-windows-phone-application-execution-model-tombstoning-launcher-and-choosers-and-few-more-things-that-are-on-the-way-part-2.aspx

This entry was posted in WP7.

2 thoughts on “WP7 TOMBSTONING – State – Launching, Closing, Deactivated and Closing Events

  1. Hello, I think your website might be having browser compatibility issues.
    When I look at your blog site in Chrome, it looks fine but when opening in Internet Explorer, it has some overlapping.
    I just wanted to give you a quick heads up! Other then that,
    awesome blog!

  2. There is no mention here of what Sleep does. (nor anywhere else). Does Sleep trigger the deactivated and does sleep recovery trigger the activated event???

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>