The Imaginative Universal

Studies in Virtual Phenomenology -- by @jamesashley, Kinect MVP and author

WP7 Tombstoning Pattern Tip

Customizing App.xaml.cs

If you are familiar with tombstoning on Windows Phone, you know there are four events in the Windows Phone application lifecycle that can be handled: PhoneApplicationService.Launching, PhoneApplicationService.Closing, PhoneApplicationService.Activated and PhoneApplicationService.Deactivated.  The first two are for normal app startup and shutdown, while the latter two are for tombstoning scenarios.

Along with these events are two different property bags that application data can be saved to. 

IsolatedStorageSettings.ApplicationSettings  is typically used for backing up persistent data that you want available every time the application starts up. 

PhoneApplicationService.Current.State is used for transient data that you only want persisted if your Silverlight application gets tombstoned.  It is the equivalent of session data in an ASP.NET web application.

In the Launching and Closing event handlers you would typically save to Isolated Storage, while in handlers for Activated and Deactivated you would want to use Current.State.  The former coding artifacts are for persistent data while the latter are for transient data.

These categories are imperfect, however.  It is possible to go through the Deactivated event but never have Activated called.  This occurs if for memory management reasons your application is permanently terminated (a permanent death scenario rather than the temporary death that tombstoning is supposed to trigger).  This can also happen, of course, if the user for whatever reason just decides not to return to your app after it gets tombstoned.

Another way to look at this is that your application is permanently terminated without ever invoking the PhoneApplication.Closing event.

In order to handle this common scenario, you must save any persistent data in the Deactivated event as well as the Closing event.

A simple pattern for structuring your code to cover all these possibilities is to make sure the code fragment for saving persistent data is called in both the Dectivated and Closing event handlers.  Similarly, the fragment for restoring persistent data should be called in both the Activated and Launching handlers.  We do this by taking care of persistent data backup and restores in two new methods: BackupPersistentData and RestorePersistentData. Those who remember the IDisposible pattern in C# will recognize some of the same coding idioms being used here.

The following code goes in App.xaml.cs:

private void BackupPersistentData()
{
    var store = IsolatedStorageSettings.ApplicationSettings;
    //save persistent data
    store.Save();
}
private void RestorePersistentData()
{
    var store = IsolatedStorageSettings.ApplicationSettings;
    //restore persistent data
}

private void Application_Closing(object sender
    , ClosingEventArgs e)
{
    BackupPersistentData();
}

private void Application_Launching(object sender
    , LaunchingEventArgs e)
{
    RestorePersistentData();
}

private void Application_Deactivated(object sender
    , DeactivatedEventArgs e)
{
    BackupPersistentData();
    var store = PhoneApplicationService.Current.State;
    //save transient data
}

private void Application_Activated(object sender
    , ActivatedEventArgs e)
{
    RestorePersistentData();
    var store = PhoneApplicationService.Current.State;
    //restore transient data
}

Sticking to this pattern has helped me to avoid a lot of unforeseen mistakes in my own phone apps.

 



Comments (13) -

Could you help me out in this issue if this makes senseSmile

I have seen other articles and comments which are broadly in accord with WP7 Tombstoning Pattern Tip but I have to admit that this post has some supplementary detail.  I'm plainspoken and trustworthy, that way everyone acknowledges what I mean.

תקליטן לאירועים זה אחד המוטיבים הכי חשובים בכל אירוע. תקליטן טוב יקבע את קצב האירוע וישים מוזיקה מובחרת לאירועים

Thank! for good post. Useful for me and another one.

I have seen other articles and comments which are broadly in accord with WP7 Tombstoning Pattern Tip but I have to admit that this post has some supplementary detail.

this article is very helpful & informative with very useful knowledge i have gotten from here thanks for it.

Netherlands Andre van der Plas

Hi James,

Nice article! I have a question about the Application_Activated method in which you both restore from isolated storage as well as from the Current.State.

I would suggest that you first check whether the information is available inside the state (containskey) and if not available resort to retrieving the info from isolated storage.

What do you think?

--Andre



Welcome to our website. <a href="http://www.lovelybag2u.com">louis vuitton handbags wholesale</a> are one of the best selling brand sunglasses around the world. <a href="http://www.lovelybag2u.com">louis vuitton handbags wholesale</a> constantly strives to support those qualities by reinventing the concepts of comfort fit and style.

Tech4mods.com has the best assortment of R4 Card, Acekard 2i, R4i, R4i-SDHC, R4DS, R4 DS and Nintendo R4.

It’s said that life has its moments. Many are brief, fading quickly from memory. Others stay with us and may even alter the course of our lives. At  www.pandorashopuk.co.uk/pandora-bracelets-c-1.html , we call these the unforgettable moments – times to remember and celebrate because they remain important. This magazine collects these moments. We hope you find them worth sharing. Take a look at our new corporate annual magazine and learn what  www.pandorashopuk.co.uk/pandora-beads-c-3.html is all about.

I am happy to have found this blog, and more happy to have long tail explained so well. Thanks.

have seen other articles and comments which are broadly in accord with WP7 Tombstoning Pattern Tip but I have to admit that this post has some supplementary detail.

I am happy to have found this blog, and more happy to have long tail explained so well. Thanks.

Pingbacks and trackbacks (7)+

Comments are closed