What’s New in Windows Phone 8.1- App Lifecycle and Navigation (Part 3 out of 5)

April 2, 2014

This post focuses on App Lifecycle and Navigation changes in WP8.1 XAML (aka as WinRT or Windows Phone Sore apps) apps and how they different from SL8.1 apps.


App Lifecycle

WP8.1 apps very similar lifecycle to Windows Store apps. This lifecycle is somewhat different from SL8.1 apps that mostly follows the WP8.0 apps lifecycle. Let’s review the WP8.1. lifecycle:

1. User taps app icon (in apps list or main screen) to run the app. Similarly to Win8.x the app is launched and Application.OnLaunched event is raised. As in Win8.x, it has LaunchActivatedEventArgs that helps to understand how the app was launched (ActivationKind) and previous execution state (ApplicationExecutionState).

2. When user navigates away from the app (incoming call or by clicking Windows/Search/Back key) the app receives OnSuspending even and then suspended in memory.
Noted something interesting in this statement? The app is suspended when user press Back key! This is different from SL apps that by default will navigate back in app stack when Back key is pressed and terminated when user navigates back from first page in the app. This default behavior in WP8.1 apps is overrideable and I will show how to override it in next part of the post.

3. When user returns to the app (by tapping app icon on the app list or main screen, clicking the Back button or using app switcher) the app is resumed and can react by subscribing on Resuming event to restore the working state if needed.
Noted something interesting in this statement? The app is resumed when user taps app icon instead of running new instance as it was in WP8.0. Also, app will get additional OnLaunched event in this case and ApplicationExecutionState will be “Running” so you can adjust your business logic.
Note: This behavior affects also SL8.1 apps: a SL8.1 app is always resumed when launched, instead of replacing any running instance.

4. Apps can be closed by user using app switcher:
In this case when app will be launched again the ApplicationExecutionState will be “ClosedByUser”.

5. The app can be terminated by the system – if the system runs short of memory it will use the priority system to determine which app to terminate. In this case when app will be launched again the ApplicationExecutionState will be “Terminated”.

To deal with app state while suspending/resuming the Frame provides [Get/Set]NavigationState methods that helps to deal navigation state.



WP8.1 XAML apps adopt Win8.x navigation model of navigating by type rather than by Uri. This helps to identify potential navigation problems on compilation stage instead of discovering them at the runtime. Basic navigation between the pages looks like the following:

Frame.Navigate(typeof(MainPage), optionalNavigationArguments);

Note: optionalNavigationArguments parameter is an object that can be passed between the pages. This parameter can be retrieved at OnNavgatedTo event hander as NavigationEventArgs.Parameter.

Every page provides a set of functions to deal with navigation events such as OnNavigatedTo, OnNavigatedFrom, etc. In addition, the Frame provides standard set of navigation functions such as CanGoBack, GoBack, CanGoForward and GoForward that can be used to navigate between the pages in the app.

In previous versions of WP the Back key used to navigate back in stack of app pages and when first page is reached to navigate from the app and terminate it. Inn WP8.1 hardware Back key navigates back to the previous app – not page! This behavior is different from SL8.1 apps when Back key still works the same way it was on previous versions of WP. This behavior can be overridden (and the code is provided by VS template) so WP8.1 apps also navigates back in the pages stack instead of navigating between the apps. The default code provided by tools looks like:

public App()



    this.Suspending += this.OnSuspending;

    //Subscribe to the Back keypress event to override default system behavior

    HardwareButtons.BackPressed += this.HardwareButtons_BackPressed;



//Handles the back button press and navigates through the history of the root frame.

//If the app is at the first/launch page, don't go back and the system will suspend the app

private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)


    Frame frame = Window.Current.Content as Frame;

    if (frame == null)





    var handler = this.BackPressed;

    if (handler != null)


        handler(sender, e);



    if (frame.CanGoBack && !e.Handled)



        e.Handled = true;



If you need to implement your own Back key navigation logic, modify this code.

When pressing Back key on the app’s first/launch page it suspends the current app. This behavior is also different from SL8.1 apps when Back key – in SL8.1 it closes/terminates the app. This behavior is not overridable.

Every page has a NavigationChacheMode property that controls whether a page will be destroyed or cached when app navigates away. By default the value is NavigationCacheMode.Disabled which means that when navigating back to the same page (for example using Back button) the app get a new instance of the page and it must be initialized. This behavior is different from SL8.1 apps where the page is preserved in the history. The override this behavior add the following code snippet to the page:

public MyPage()




    //Require saving the page in cache

    this.NavigationCacheMode = NavigationCacheMode.Required;



protected override void OnNavigatedFrom(NavigationEventArgs e)




    //When navigating backwards the page should't be saved in cache so change the NavigationCacheMode

    if (e.NavigationMode == NavigationMode.Back)

        this.NavigationCacheMode = NavigationCacheMode.Disabled;



That’s it for now. Next time I will be talking about new features such as geofencing, sharing using share contract, background tasks, tiles and toasts and many more.


Stay tuned,


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>