Tombstoning

YLOD works with reflection to extract all required information from the object you provide. It has no knowledge about where your object comes from, how and where you use it, whether it is serializable and how, or even how to create and configure it. That means that it's your responsibility to properly handle deactivation and tombstoning situations, and to restore the object reference for the options dialog in case your application is tombstoned when the user currently views the options page. Luckily, it sounds much harder than it is.

Store Your Values upon Deactivation and Closing

When your application is deactivated (or closed), you should store your options or settings. You are of course completely free how to do that, and this indeed has nothing to do with YLOD at all. A simple data container class with a few user settings could be properly saved in App.xaml.cs like this:

// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
    IsolatedStorageSettings.ApplicationSettings["SampleOptions"] = SampleOptions;
    IsolatedStorageSettings.ApplicationSettings.Save();
}

// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
    IsolatedStorageSettings.ApplicationSettings["SampleOptions"] = SampleOptions;
    IsolatedStorageSettings.ApplicationSettings.Save();
}

Restore on Activation

When your application is activated, check for the IsApplicationInstancePreserved flag to determine whether you need to actually restore anything (this is new in the "Mango" release). If this flag is true, you don't need to do anything for the options page to continue to work. Again, up to this point this is nothing particularly special to the use of YLOD and just standard lifetime management on the phone.

If the IsApplicationInstancePreserved flag however is false, then this means that you need to restore your options from wherever you've stored them, and tell the options service about the restored reference. For the simple case mentioned above continued, the following is sufficient:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    // if you do not restore the options reference when the app is returned to from tombstoning,
    // the options dialog will navigate back to the previous page automatically
    if (!e.IsApplicationInstancePreserved)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("SampleOptions"))
        {
            SampleOptions = (SampleOptions)IsolatedStorageSettings.ApplicationSettings["SampleOptions"];
        }

        // restore the option values in the options service
        OptionsService.Current.RestoreAfterTombstoning(SampleOptions);
    }
}

As indicated by the comments in the snippet above, the following logic applies:

If the options page is being navigated to, and it detects that a tombstoning operation happened, the engine tries to reinitialize all options from the object reference in the OptionsService automatically. If you failed to provide a restored options reference (e.g. you do not have code like the one shown above in place), the options page has nothing to show or work on and will automatically invoke a back navigation. This means that if you do not properly restore the underlying options reference upon tombstoning, the user will be taken back to whatever page initiated the navigation to the options page before.

Note: Although it theoretically is possible for the options service to save and restore custom configuration options internally, this is not done. This means if you are using custom configuration parameters for the service itself, then you have to use the RestoreAfterTombstoning overload that takes an OptionsConfiguration instance to restore that configuration too.

Additional Considerations

If you are making use of the extensibility options of YLOD as described in other parts of this documentation, you also need to restore the view factory and extender (if applicable) you have configured after a tombstoning situation. Again, YLOD does not know how to serialize and restore these objects, or if and how they can be recreated and configured properly, so it's your responsibility to do that. That means that in this case, your activation code would look similar to this:

private void Application_Activated(object sender, ActivatedEventArgs e)
{
    // if you do not restore the options reference when the app is returned to from tombstoning,
    // the options dialog will navigate back to the previous page automatically
    if (!e.IsApplicationInstancePreserved)
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("SampleOptions"))
        {
            SampleOptions = (SampleOptions)IsolatedStorageSettings.ApplicationSettings["SampleOptions"];
        }

        // also restore the view factory and factory extender if you're making use of these features!
        OptionsService.Current.OptionsViewFactory = new MyOptionsViewFactory();
        OptionsService.Current.OptionsFactoryExtender = new MyOptionsFactoryExtender();

        // restore the option values in the options service
        OptionsService.Current.RestoreAfterTombstoning(SampleOptions);
    }
}

Last edited Nov 20, 2011 at 4:41 PM by Mister_Goodcat, version 2

Comments

No comments yet.