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

Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in Essentials, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

4 Responses to Fast Application Switching Made Easy

  1. Pingback: WPDev Sin: Gluttony « Windows Phone Love & .NET Ramblings

  2. Peter Wone says:

    Hi Jessie. Your comments on the excellent new facility for debugging returns from dormancy have sparked a thought on how the debug story can be greatly improved for location aware apps.

    Mango lets you control the position reported by the Location service. Finally, it’s possible to debug location event handling.

    However, this is not enough for regression testing. What is required is the ability to storyboard a simulated journey. Ideally, the storyboard would manage position, heading, speed, acceleration, jerking and orientation, with enough of a physics model to allow some of these factors to be computed from others – if you specify speed at two times, the acceleration between should be assumed linear for simplicity unless a jerking function is supplied (acceleration A = dV/dT, jerking J = dA/dT).

    In fact this is most often applied to motor vehicles so the jerking function would be kT^2 because the force goes up with the engine revs which is a function of T.

    No need to go into it in any more detail than that because you can almost certainly get mature easing functions (these are basically easing functions if you ask me) from any modern game framework.

  3. Pingback: Fast Application Switching Made Easy | WP7 Developers Links

  4. Pingback: Windows Phone Mango Fast Application Switching Made Easy – www.nalli.net

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=""> <strike> <strong>