Windows Phone 7 (Mango) Tutorial - 13 - What is Tombstoning?


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.


10 comments

  1. Hi Kunal. There is a conceptional problem with your approach. When you start the application from the start menu, you are actually NOT supposed to continue a previous session but give the user the impression they started a new instance. The following is a quote from the Best Practices section of the Execution Model docs:

    "Ensure that when your application is launched by the user from Start or the installed applications list, the user is taken to a consistent, launch experience. It should be evident to the user that they are experiencing a new application instance.

    When the user launches a new instance of your application, it is acceptable to present the user with some information about a previous instance, for example, a list of recently opened documents, but the user should not feel like they are continuing a previous session."

    (Source: http://msdn.microsoft.com/en-us/library/ff817009(v=VS.92).aspx)

    So what you demonstrate is what should be done when the user returns to the application using the hardware back button (can be done in the emulator too), but not when you start it from the application list.

    ReplyDelete
  2. Thank very Much for describing a lot:
    Kunal is there any artical to store data in SQLCE so that I can see the my Real DB where my data will goes..!

    Because I had tried with LocalDatabaseSample which is given by MicroSoft but still it is a part of the Isolation so how I would know phisically where is my DB so that I can sync this phone data to my Desktop DB the both DB are same..!!

    ReplyDelete
  3. Hi Kunal, Thanks for sharing these elaborative articles with us, I am really enjoy reading them.

    Is it possible to share the source code for above topic? I couldn't find implementation for MainPageViewModel, can you explain this or share any resource which explain this?

    Thanks in advance
    Saqib

    ReplyDelete
  4. Hello Saqib,

    I will share all the Source Code of each tutorial chapter in a separate post. Please wait for some days. Stay tuned...

    ReplyDelete
  5. hi kunal...
    can you please tell me about
    public MainPageViewModel MainPageViewModel { get; set; }
    what is it exactly?

    ReplyDelete
  6. public MainPageViewModel MainPageViewModel { get; set; }

    This is nothing but a normal CLR property which has the instance of the ViewModel created in the Constructor and assigned to the DataContext of the page:

    // Constructor
    public MainPage()
    {
    InitializeComponent();

    if (!IsolatedStorageSettings.ApplicationSettings.Contains("ViewModel"))
    {
    MainPageViewModel=new MainPageViewModel();
    DataContext = MainPageViewModel;
    isFirstInstance = true;
    }
    }

    ReplyDelete
  7. Hi kunal,

    How to use MainPageViewModel class bcz this line :

    public MainPageViewModel MainPageViewModel { get; set; }

    shows error in code. Plz mention namespace for same.

    Thanks !!

    ReplyDelete
  8. Ok it works better for UI Elements. But is it work for Variables

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top