Computer Magic Logo
Saving transient data

Monday, April 11, 2016

Published by Aristotelis Pitaridis

Sometimes the execution of the application may be interrupted. This can be done for a lot of reasons like when we have an incoming call. In that case we usually want to save the state of the application or even save data that the user has typed in our application so that the next time the application will be activated, the user will continue what he was doing before the interruption.

Let's make an example in order to implement this functionality for a simple application. We suppose that our application will have an Entry (textbox) control that the user may type a string and we will have to save it's value and and load it again in case we have an interruption.

We will use the OnSleep event of the Application class in order to know when our application will be interrupted so that we will save the required information.

public class App : Application
{
    public App()
    {
        MainPage = new Page1();
    }

    protected override void OnSleep()
    {             

    }
}

Now that we overrided theĀ OnSleep event we will create a property where we will temporary save the important value until the application interruption occurs.

public class App : Application
{
    public string ImportantValue { set; get; }

    public App()
    {
        MainPage = new Page1();
    }

    protected override void OnSleep()
    {             

    }
}

Now we are ready to save and load the value. The saving will be in the OnSleep and the loading will be in the constructor of the Application class.

public class App : Application
{
    public string ImportantValue { set; get; }

    public App()
    {
        // Set the ImportantValue property to be equal to the saved value 
        // if we have a value in the Properties dictionary
        if (Properties.ContainsKey("ImportantValueKey"))
        {
            ImportantValue = (string)Properties["ImportantValueKey"];
        }
        MainPage = new Page1();
    }

    protected override void OnSleep()
    {             
        // Save the value of the ImportantValue property
        Properties["ImportantValueKey"] = ImportantValue;
    }
}

Our application is ready to save the transient data but we have not used this implementation in our application. We suppose that the Page1 content page that we loaded in the constructor has an Entry (textbox) control and we want to save it's contents during an application interruption.

public partial class Page1 : ContentPage
{
    public Page1 ()
    {
        InitializeComponent ();

        // Get a copy of the current application class
        App app = Application.Current as App;

        // Set the text of the Entry control to contain the value
        // that we stored in the ImportantValue property
        MyEntry.Text = app.ImportantValue;

        // Define the function which will be used when the user 
        // change the value of the Entry control
        MyEntry.TextChanged += MyEntry_TextChanged;
    }

    private void MyEntry_TextChanged(object sender, TextChangedEventArgs e)
    {
        // Set the value of the ImportantValue property
        // to contain the new value of the Entry control
        App app = Application.Current as App;
        app.ImportantValue = MyEntry.Text;
    }
}

Now we can execute the application, type a value to the Entry control and after the application termination we will be able to start the application again and the last typed value will be in the Entry control.