Fast Application Switching Made Easy

Mango From Scratch

One of the key features in Mango is Fast Application Switchingstopwatch

This is a radical change to the life-cycle of applications, and one of the very first things you’ll want to do, when upgrading your Windows 7 application to Mango will be to support Fast Application Switching.

Here’s the deal.  In Windows 7, if your application was running and the user started a second application (e.g., by pressing the Windows key or taking a call) your application was immediately tombstoned.  When the user returned to your application (using the back key) you would restore state from isolated storage. That can be a bit time consuming.

In Mango, when your application is running and the user starts a second application you are not tombstoned, you are put into a Dormant state where your image is maintained.   (Note, the application is stopped and disconnected, so you still have to restore sockets, update timers, etc.) 

Key to making all this work is knowing whether you are being restored from being Dormant or from having been Tombstoned.  That turns out to be wicked easy.  Here’s an incredibly simple program that illustrates how it is done, and how you can test it.

Create a new Windows Phone 7.1 application.  Put a single, centered TextBlock named Message with no text.  That is your UI. 

Open App.xaml and create a static property to hold the status,

public static bool WasTombstoned { get; private set; }

Locate the Application_Activated method in App.xaml and add this test:

private void Application_Activated( 
   object sender, 
   ActivatedEventArgs e )
{
    WasTombstoned = ! e.IsApplicationInstancePreserved;
}

 

Note the inversion: you were tombstoned if IsApplicationInstancePreserved is false.

Finally, we’ll indicate how we were restored by setting the Message TextBlock you created in MainPage.xaml. 

public MainPage()
{
    InitializeComponent();
    Message.Text = App.WasTombstoned ? "Restoring..." : "Fast!";
}

 

Running this you should see that you are typically coming back from a Dormant state using Fast Application Switching.  To make sure it is working, however, you’ll want to force tombstoning.  Easily done:  right click on the project in Visual Studio, and choose Properties.  Click on the Debug tab and tick the CheckBox that says Tombstone upon deactivation while debugging.  Try the application again and this time you should see Restoring….

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 Essentials, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.