📜 ⬆️ ⬇️

Application recovery when starting from the start screen without losing the previous state in WP8

Windows Phone 8 brought such a wonderful opportunity as a quick resumption of the application state after it was restarted from the application list or through the home screen tile with full state and navigation history inside the application (to enable subsequent navigation using the Back button as if the application was nowhere folded).

If you have already tried to use Windows 8 and applications from the Windows Store, you may have noticed that when you start the application from the start screen, you will return exactly to the place of the application where you left it. This is the result we will achieve in our application for Windows Phone 8 .

So, let's begin.

WMAppManifest.xml


First, let's open the manifest file of our application (by clicking on it in Solution Explorer and selecting View Source, in the English edition of Visual Studio). All we need to do is add an attribute called ActivationPolicy with the value Resume to the DefaultTask element. Like this:
')
<DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume" /> 

By and large, with this change, we ensured that our application, when restarted via the start screen / list of applications, began to recover to its previous state, plus switching to the page specified in the DefaultTask element's manifest attribute's NavigationPage attribute. Immediately after this, the navigation history of our application looks like this:
image
This slightly accelerated the restart due to the fact that the system no longer clears the state and does not start the application from scratch. But, we go further.

App.xaml.cs


Now we have to cancel this unnecessary transition to the main page after restarting the application. To do this, we need to slightly edit the App.xaml.cs file (you will notice that the SDK for some reason does not want to work as described on MSDN, so we will go a little different way).

To begin with, let's add a private non-static field of type System.Boolean to our App class and name it _reset.

 private bool _reset; 

The next step is to find the private InitializePhoneApplication method (it is automatically created by the default template) and set up the initialization of the RootFrame, defining our fragments for the Navigating and Navigated events.

 RootFrame.Navigating += RootFrame_Navigating; RootFrame.Navigated += RootFrame_Navigated; 

The final step will be to modify the bodies of the handler methods of these events.

 private void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e) { if (_reset && e.IsCancelable && e.Uri.OriginalString == "/MainPage.xaml") { e.Cancel = true; reset = false; } } private void RootFrame_Navigated(object sender, NavigationEventArgs e) { _reset = e.NavigationMode == NavigationMode.Reset; } 

And that's all, the goal is achieved. Now, after the application is resumed when it is restarted, the user will go exactly where he left the application.
image
In just a few minutes, we have made our application much more pleasant to use.



You can also play around and, for example, when resuming, clear the history of the Back button, while maintaining only the state of the current page. To do this, you need to add the following line in the Navigated event handler:

 if (e.NavigationMode == NavigationMode.Reset) while (RootFrame.RemoveBackEntry() != null); 

image
That's all, thank you for your attention.

Note


The original article lacked some points from the official documentation , which I thought it appropriate to add to the court of readers.

Source: https://habr.com/ru/post/157379/


All Articles