4
votes

I'm reading through Windows Phone 7.5 Unleashed, and there is a lot of code that looks like this (in the code-behind for a page):

bool loaded;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (!loaded)
    {
        DataContext = new SomePageViewModel(State);
        loaded = true;
    }

    ((SomePageViewModel)DataContext).LoadTransientState();

    ...
}

...

The idea is that loaded will be false when resuming from a tombstoned state, so we know that we want to rebuild the view model.

My question is: Why load the transient state outside of the if block? If our memory has not been thrown away (i.e., the application has not been tombstoned), can't we just keep using the old view model without reloading its state?

Should transient state always be loaded when a page is navigated to, or just when resuming from a tombstoned state?

1

1 Answers

1
votes

I'm not familiar with the book and from the code you've shown it's hard to say if this is correct or not. It comes down to what LoadTransientState actually does.

The approach you want to take to handling tombstoning is making sure that (as long as it makes sense within the application*) the app/page should be in the same state when the user returns to it as when they left.

So, if LoadTransientState is only concerned with the persistence of information while tombstoning, then it may be appropriate to move it inside the if block.
If it deals with some other form of transient data then probably not.

Hopefully LoadTransientState will contain some logic to make sure it's not setting (or resetting) data that already exists (or has been set) internally and so will avoid any intended consequences of being called unnecessarily.

*Some obvious exceptions being live data, timers (possibly), real-time info, etc.