The Imaginative Universal

Studies in Virtual Phenomenology -- @jamesashley

WP7 Tombstoning Pattern Tip

August 22
by James Ashley 22. August 2010 16:20

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.

 



Tags: , ,

Windows Phone | Silverlight | Customizing App.xaml.cs

Comments

9/5/2010 6:40:41 PM #

pingback

Pingback from cisforcoder.wordpress.com

Windows Phone 7 Developer Links « C Is For Coder

cisforcoder.wordpress.com

11/12/2010 7:16:19 PM #

aol directory

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

aol directory United States

11/20/2010 4:03:32 AM #

drain clearing

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.

drain clearing United Kingdom

12/16/2010 2:29:21 AM #

תקליטן

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

תקליטן Israel

1/26/2011 4:42:03 AM #

Amanda

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

Amanda United States

6/8/2011 5:18:54 AM #

Pandora Bracelets

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.

Pandora Bracelets United States

6/16/2011 4:58:00 AM #

louis vuitton bag

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

louis vuitton bag People's Republic of China

6/21/2011 12:34:57 PM #

pingback

Pingback from chaokuaidi.com

超快递 beta版 » Windows Phone 7 资源汇总(超全)

chaokuaidi.com

6/23/2011 6:46:41 AM #

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

Andre van der Plas Netherlands

6/25/2011 7:43:05 AM #

louis vuitton luggage sale



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.

louis vuitton luggage sale People's Republic of China

6/29/2011 9:57:54 AM #

r4 ds

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

r4 ds United States

7/3/2011 9:28:47 PM #

Pandora Bracelets

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.

Pandora Bracelets People's Republic of China

7/11/2011 4:46:32 AM #

tiffany rings

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

tiffany rings People's Republic of China

7/11/2011 10:15:22 AM #

אחסון אתרים

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.

אחסון אתרים United States

7/15/2011 5:38:59 AM #

tiffany bangles online

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

tiffany bangles online People's Republic of China

7/18/2011 6:40:32 AM #

pingback

Pingback from programmersgoodies.com

WP7 – Resume from the page I assigned - Programmers Goodies

programmersgoodies.com

7/18/2011 6:40:34 AM #

pingback

Pingback from programmersgoodies.com

WP7 – Resume from the page I assigned - Programmers Goodies

programmersgoodies.com

12/7/2011 8:08:19 PM #

pingback

Pingback from blogs.interknowlogy.com

InterKnowlogy Blogs  » Blog Archive   » Windows Phone 7 – Tombstoning

blogs.interknowlogy.com

5/17/2012 11:15:09 PM #

pingback

Pingback from dancecoder.com

Windows Phone 7 资源汇总 | DanceCoder

dancecoder.com

Comments are closed