Mobile phones have very limited resource as compared to a desktop PC and hence it is very difficult to run multiple applications simultaneously. Also, suppose if multiple resources run simultaneously, it will eat up the battery life and make your phone stop working after certain period of time.

 

So what to do? To overcome this situation, tombstoning comes into picture. It saves the current data and then moves the app to the background or closes the app after saving all the data. This calls as Tombstoning. In this post, we will discuss on the same.

 

Index - Windows Phone 7 (Mango) Tutorial

 

Background

Let's take a case study. Suppose, you are filling up a form in your Windows Phone application and at that time a call came and your application will pushed into background or you accidently pressed the home button present in your phone hardware. Due to this, you will lose all your entered data.

 

So, how to resolve this issue? We can tombstone our application at the time of closing or deactivation and load them on launch or activation. If you read my previous post, you can guess about the implementation. Yes, you can store data in your Isolated Storage and later load them from there.

 

In this post, we will see a better and easy way to use the isolated storage to save our data.

 

Normal Application Behavior

Before starting with the actual implementation, let us go with the normal behavior. First of all, we will create our MainPage UI with two TextBox binded to two string properties called "Firstname" and "Lastname" present inside viewmodel. As we are using TextBox, don't forget to set the Mode=TwoWay attribute at the time of binding.

 

Need the code for reference? Find it here:

 
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <Grid.RowDefinitions>
        <RowDefinition Height="70"/>
        <RowDefinition Height="70"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBlock Text="Firstname :" Grid.Row="0" Grid.Column="0"/>
    <TextBox Text="{Binding Firstname, Mode=TwoWay}" 
             Grid.Row="0" Grid.Column="1"/>
    <TextBlock Text="Lastname :" Grid.Row="1" Grid.Column="0"/>
    <TextBox Text="{Binding Lastname, Mode=TwoWay}" 
             Grid.Row="1" Grid.Column="1"/>
</Grid>

 

Once the XAML has been modified, build and run the application. When the app loads, you will find the screen with the two TextBoxes as we designed earlier. Enter text into the TextBoxes (in our case they are Firstname and Lastname). As we are using two way data binding mode, the text will get store into the properties present in the viewmodel.

 

Now click the "Home" button present in the hardware device (as shown below [2]) which will send the application to the background under lock screen. Now click the arrow as shown in the second image. This will navigate you to the installed application list. Click our application hosted under the list.

 

image     image     image

 

This will relaunch our application and you will notice that, the application lost all the values that we entered earlier.

 

image

 

 

Tombstoning the Application

To overcome the situation, we will implement code to save the state of the application to the user's isolated storage. If you already read the last chapter, it is easy for you. You can use a serializer to store the instance and deserialize it when back.

 

In this article, we will discuss with a simple sample. Here instead of using the serializer, we will use the ApplicationSettings to store the state.

 

We will first create a property to store the MainPageViewModel instance. We will also create a private variable to flag whether it is first instance. Then in the constructor, we will check if the ApplicationSettings has the key for the viewmodel and based on that, we will create the first instance of the viewmodel and assign it as DataContext of the view. Have a look into the code:

 
public partial class MainPage : PhoneApplicationPage
{
    public MainPageViewModel MainPageViewModel { get; set; }
    private bool isFirstInstance;
 
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        
        if (!IsolatedStorageSettings.ApplicationSettings.Contains("ViewModel"))
        {
            MainPageViewModel=new MainPageViewModel();
            DataContext = MainPageViewModel;
            isFirstInstance = true;
        }
    }
 
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        IsolatedStorageSettings.ApplicationSettings["ViewModel"] = MainPageViewModel;
        base.OnNavigatedFrom(e);
    }
 
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (isFirstInstance) { return; }
 
        MainPageViewModel = 
            IsolatedStorageSettings.ApplicationSettings["ViewModel"] as MainPageViewModel;
        DataContext = MainPageViewModel;
        base.OnNavigatedTo(e);
    }
}

 

When we navigate away from the page, we will store the viewmodel instance to the application settings present inside the isolated storage. This can be implemented in the OnNavigatedFrom event.

 

When we return back to the application, we will get the value by passing the key to the ApplicationSettings and we will get the instance of the viewmodel very easily. Only thing that we have to do is, just setting the viewmodel instance to the DataContext of the view.

 

image image image

 

Now once we do the same steps again and re-enter to the app from the application list, we will see that the values updated to the view from tombstoning.

 

image

 

 

End Note

Hope, this information helped you to understand the tombstoning process and mainly the process of using the IsolatedStorage to store and retrieve data. Stay tuned for my coming articles. Follow my blog and facebook page to be updated on latest articles, tips and news.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.