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...

Upvotes: 0

Views: 1008

Answers (3)

Jim O'Neil
Jim O'Neil

Reputation: 23764

To add to the other answers (skip to bottom for TL;DR version)...

If you're using the C#/XAML Grid or Split templates or adding anything but a Blank Page to a project, you'll be pulling in the SuspensionManager 'helper' class which sets up most of the plumbing for you.

Each of the non-blank pages extends LayoutAwarePage, which provides a OnNavigatedTo implementation like Jerry mentioned. In each page though, you only need to fill in the body of a LoadState and SaveState method - putting whatever you want to persist into a pageState parameter. And it persists the page the user was on without you doing anything - you just need to add any additional data you want to save (and restore).

The pageState gets stored in a file within your application directory (versus using Settings). If you go manual with Settings you do have to make sure everything is a Windows Runtime Type (primarily just simple types and arrays), but with pageState it can serialize objects as well.

Check out the sample walkthrough Manage app lifecycle and state for some real code and further explanation.

Upvotes: 1

Jerry Nixon
Jerry Nixon

Reputation: 31813

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.

Upvotes: 1

Martin Suchan
Martin Suchan

Reputation: 10620

Just remember the type of last page your app was on when it was suspended, and after the new start navigate to the proper page in your App class. This should not be so hard to implement.

Upvotes: 1

Related Questions