Fast Switching and Page State in Mango

Mango From Scratch

Mango introduces fast application switching.  To implement this feature a new stage in the Sleeping Student lifecycle has been added: Dormant.  Applications are put in the dormant state when the user navigates forward, away from the application (e.g., using the Start button).

In the Dormant state all execution of the code is stopped but the application remains in memory. If the user navigates back into a dormant application, the application resumes and each page’s state is restored automatically.

Unfortunately, if the phone runs low on memory, your application may be tombstoned. In that case, the application is terminated, but if the user returns to the application you are responsible for making it appear as if the application has been alive all along.  Fortunately, tombstoned applications do retain their State dictionary, and you can use that to restore the page state.

To see this at work, let’s create a very simple application that consists of a prompt, a TextBox and a Button,

    Content="Go to page 2"
    Margin="128,4" />
    Text="Page Data:"/>
    Text="" />


The button is used to navigate to a second page (from which you can return to the first page using the back button),

private void GoToPage2_Click( 
    object sender, RoutedEventArgs e )
        new Uri( "/Page2.xaml", UriKind.Relative ) );


When you navigate away from the page the OnNavigatedFrom method is called.  If the navigation is backwards, the page will be disposed of and there is no need to store the state. Otherwise, we store the state in the State object, which is a dictionary that will persist through tombstoning (but not through application termination).

protected override void OnNavigatedFrom(
    System.Windows.Navigation.NavigationEventArgs e )
    base.OnNavigatedFrom( e );

    if ((e.NavigationMode != 
        State["PageData"] = PageData.Text;


When the page is navigated to, the OnNavigatedTo method is called. We will maintain a Boolean to indicate if the user has navigated here due to a launch event (_newPageInstance == true) or after tombstoning. 

If it is not a new page, then we check to see if the page’s field is null or empty. If so, we check to see if there is data in Page State, and if so we restore.

protected override void OnNavigatedTo( 
    System.Windows.Navigation.NavigationEventArgs e )
    base.OnNavigatedTo( e );
    if (_newPageInstance)
        if (String.IsNullOrEmpty( PageData.Text ))
            if (State.Count > 0)
                PageData.Text = State["PageData"].ToString();
    _newPageInstance = false;



Note that in this example we are restoring a single field. A far more efficient way to approach this is to bind all the fields to a view model or other object and then store that object in PageState and restore that object, using NotifyPropertyChanged to update the UI.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Data, Essentials, Mango, Mini-Tutorial, WindowsPhone. Bookmark the permalink.

7 Responses to Fast Switching and Page State in Mango

  1. Pingback: New Lumia owner,operational questions - Windows Phone Forums at

  2. Pingback: Fast Switching and Page State in Mango | WP7 Developers Links

Comments are closed.