Search code examples
c#windows-8lifecyclewindows-store-appsterminate

How can I navigate back to the previous page in the OnLaunched() event?


If my app is terminated by Windows (e.g, if it has been suspended, but then Windows needs to free up memory so it preempts my app), when it is restarted, the OnLaunched() event occurs and I can test whether it was Terminated (or the user simply closed it):

    . . .
        if ((args.PreviousExecutionState == ApplicationExecutionState.Terminated) ||
            (args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser))
        {
        }
    . . .

If terminated, I'd like to resume at the previous location/page, rather than at the initial page (if different). How can I do this? Pseudocode might be:

if (CurrentPage != LastSavedPage)
{
    CurrentPage = LastSavedPage;
    // or: Frame.Navigate(typeof(LastSavedPage)
}

UPDATE

So this is my take on what I should do, but I still "have a doubt about it" (see comment and code following it)

// OnNavigateTo in each page:
Settings.Values["CurrentPageType"] = this.GetType();
Settings.Values["CurrentPageParam"] = args.Parameter; 
//Will the line directly above work even if args.Parameter is empty, or must I do something like:
if (null != args.Parameters)
{
     Settings.Values["CurrentPageParam"] = args.Parameter; 
}

//OnLaunched in app.xaml.cs
                if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    if ((localSettings.Values.ContainsKey("CurrentPageType")) &&
                        (localSettings.Values.ContainsKey("CurrentPageParam")))
                    {
                        rootFrame.Navigate((Type)localSettings.Values["CurrentPageType"],
                                                                     localSettings.Values["CurrentPageParam"]);
                    }
                }

UPDATE 2

I get an exception with this code:

    ApplicationData.Current.LocalSettings.Values["CurrentPageType"] = this.GetType();
    ApplicationData.Current.LocalSettings.Values["CurrentPageParam"] = args.Parameter;

An exception of type 'System.Exception' occurred in mscorlib.dll but was not handled in user code WinRT information: Error trying to serialize the value to be written to the application data store Additional information: Data of this type is not supported. If there is a handler for this exception, the program may be safely continued.

If I append ".ToString()" to the call to GetType(), it doesn't blow up...


Solution

  • Sure! When you override OnNavigatedTo you need to persist two things:

    1) The type of the current class, something like:

    Settings.Values["CurrentPageType"] = this.GetType();
    

    2) The params passed to the current page, something like:

    Settings.Values["CurrentPageParams"] = e.Parameters;
    

    The first caveat is that your parameters must be serializeable. That's up to you.

    The second consideration is if your applications relies on the backstack for navigation. if so, then you will need to persist more than just the current page's type, but the breadcrumb behind it. That's up to you, too.

    Then it's just something like:

    Frame.Navigate(
        (Type)Settings.Values["CurrentPageType"], 
        Settings.Values["CurrentPageParams"]);
    

    Please check for nulls and stuff like that. But, ion a nutshell, that's how.