Tombstoning and Task Switching

In a previous blog post I was talking about what happens when navigating from a page within my application to another page within my application. Also I was talking about how to preserve the state of individual page values by using “PhoneAppService.State[ ]”. That way I was able to save individual pieces of information. But that only works until the application has been removed from memory. I am going to have a look at what happens when the user clicks the Back button or the Start button and actually leave the application.

As far as the Windows Phone 7 Operating System is concerned, those two actions of clicking the Back button or the Start button are different, even though to the end user they may appear to be the same. When the user opens an application from the start screen the application has been launched, it is in a launched state. When the user, after interacting with the application, now clicks the Back button on the phone, the application goes into a closed state. At that moment the application is removed from memory. Suppose the user opens up an application from the start screen, interacts with the application, then clicks the Start button on the phone a second time and launches another application. At this moment the application is considered to be deactivated. Not closed, but deactivated. To the end user the effect of using both the Back button and the Start button appears to be the same. The application is no longer visible. But even when the application has been deactivated it has been removed from the phone’s memory. So, what is the difference then between closed and deactivated? It seems to be the intent of the user. Closing the application denotes that the user is finished interacting with. Deactivating denotes that the user is just temporarily switching to another application but intents to come back to the application later. So, while the practical implication is the same, the distinction allows the developer to decide how to handle this subtle difference.

When the user clicks the Start button again and relaunches the application, the application is set to be in an activated state. All unsaved data though has been removed from the phone’s memory as well, just like the application was removed from the phone’s memory.

Fortunately there are some events, so that a developer can handle this exact situation. When the user either clicks the Back Button or the Start button, or when the user returns to the application after either of those events. There are events that the developer can write code to deal with these situations and save off data and then restore it once the application has been relaunched or reactivated.

This process of saving and then restoring the state of an application that has been removed and then reopened on the phone is called Tombstoning.

This blog post is about how to add event handlers to save and retrieve the data and the settings that described the current state of the application.

The easiest way to illustrate this concept is in a couple of lines of code creating a textbox in an application and allow the user to type into it. As the user is typing I am going to save that temporarily. But then when the Back button or the Start button is clicked I am going to save that in a more permanent place in Isolated Storage.

To see that in an example, I am creating a new project where I drag and drop a textbox to my main form, and in the properties window clear out the text. Then I create a TextChanged event handler and add some code:

With this I am going to save anything the user types in into a temporary storage area, in case they do something else like move to another page within the application or exit.

Next thing I do is creating a Loaded event for the PhoneApplicationPage:

If a value exists I am going to pull it out:

This will handle the situation like in the earlier mentioned blog post. When the user navigates to another page inside the application and then comes back to the page the value will be retained. What it doesn’t solve is when the user clicks the Start button or the Back button and then tries to come back to the application. This data won’t be available in that situation. So, let’s get that done.

First I am going to my Solution Explorer, find the App.xaml.cs file and open it by double clicking it. There are a number of events with no code that have already been defined:

Ok, let’s move on and create some helper methods right before it says “Phone application initialization”:

Their job is simple: They’re going to take whatever was in StateBack that I have already saved in my MainPage.xaml.cs file and save it into Isolated Storage. I am creating a reference to the current PhoneApplicationService so that I can retrieve the State value in my temporary storage area. Then I am creating a reference to the IsolatedStorageSettings which gives me access to the ApplicationSettings that are state in Isolated Storage. Then I take that value that is saved in a temporary state and save it in a permanent state:

Now that I have saved those values I am going to write some code to retrieve those values. Just like before I am referencing to the PhoneApplicationService and the IsolatedStorageSettings, only this time I am going to try to get “MyValue” out of IsolatedStorageSettings and in case that is successful it’s going to execute that code block where myValue is set in phoneAppService temporary storage:

Basically I am taking myValue, if it exists, and put it from a short term memory to a long term memory.

Now that I have built SaveState and LoadState I need to call each of these appropriately inside of those already defined events:

Last thing I want to do is making sure that I don’t get an exception. I am making sure that there is a key value available. And I am going to do that on my MainPage.xaml.cs file:

So let’s run it and see if it works:

When I click the Start Button and come back to my application my value “andrea” is still there. The same is true for the Back button and then coming back to the application.


To be continued…



  1. No trackbacks yet.

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: