Fast Application Switching in Windows Phone

  • We’ll start by understanding how does fast application resume which is the HEART of the new release.
  • We’ll explore the execution model which features the fast resume (Dormant state)
  • We’ll look on how does Fast Application Switching affect our application when it comes to application life cycle

What is “Fast Application Switching”?

Microsoft’s platform in Windows Phone 7.5 is to provide a seamingless experience to the user. These are speed and responsiveness.

Most of the users do not just use a single application and use it on their smart
phones, most of them uses multiple applications and switches them back and forth.

Background processing must not affect the battery phone…

The phone should be smart enough to balance the overall system health and
capabilities and that is the reason why Microsoft release Mango release when 
it comes to multi tasking

The consequences..

Even in Mango update, the phone will only allow one application to run at any given time, any application that is no longer in the foreground is actually deactivated. In Windows Phone 7 execution model that is called tombstoning. For example, when the user closes the application by 
pressing the start button, the first application goes in tombstone mode. What does it mean? It means that the operating system saves the application state and dictionary state for later restores. It kills the threads and processes of the application. Now when the user goes back to the application using the hardware button
the OS will resuscitates the application back to the state it was in before being tombstoned. So we're like tricking the users that it is running in the background while in reality it is a brand new process that is being created upon activation and memory state has been restored from the saved state data.

The application lifecycle that Windows Phone 7 implements already addresses the two goals preventing the background processes from impacting the foreground and affecting the battery power. Now the problem is that it takes for a long time for an application to resume from a tombstone mode. If you already have a Windows Phone 7, you’ve probably experienced when you’re restoring your application that it is slow.

The solution

For the Windows Phone 7 developers you notice that there is a new state that has been added in the Application Life Cycle, which is the Dormant State, this is the new and enhanced model for Windows Phone 7.

What the Dormant State does is that it no longer kills the application's threads and processes instead the threads are not destroyed but halted and the application image is kept intact in memory. In other words, the application preserves the application instance if the application is reactivated it instantly resumes as if it is already loaded in the memory. So there is no need to go through to the process of restoring the application state from the saved dictionary as in the case of tombstoning that is what Fast Application Switching is. Also associated with Fast Application Switching is the new ability of the user to quickly jump across applications by pressing and holding the back button which will display the previous listed applications. Unfortunately, this feature is not yet available in the emulator.

So how does Mango Life Cycle works…?

Let’s say we have a running application and then the user presses the start or search button and then user launches a second application from the start menu, in Mango the effect of this action is for the first application to go to Dormant state, the process is not terminated but phone resources are detached and application threads and timers are suspended. The application goes into frozen state, it cannot execute code so it cannot consume battery power and it does not hold on to phone resources.

Before that happens, the deactivated event is raised by the platform and we can handle this event to save our own application data which needs to be saved so that if the application is later activated it will look to the user as if we continued the application running in the background. Now if the user navigates back to the application the activated event is first raised and then the application is fast resumed. Meaning, it restarts instantly as if there is no need for the OS to restore state into memory. Processes are already created, the frameworks are already initialized and application state and content are already present in the memory.

NOTE: While the application is in dormant state, if the user keeps launching new applications eventually the operating system will start running low in memory and it will start reclaiming some of the memory that is used by the dormant applications whose images are maintained and the OS does that by tombstoning dormant application as needed, starting with the one that are frozen first.

Again, tombstone mode means that the application threads are terminated although state dictionary are saved so the application can later be revived.  From there, if the user does return to the tombstone application then the observed behavior is the same as in Windows Phone 7 OS which is that the application will typically resume with the necessary delay to recreate threads and processes and restore state into memory. Note that the activated event is raised in the same way whether we are returning from a dormant state or the tombstone state. The alternative scenario is if the tombstoned app never gets reactivated, Mango will maintain state for a maximum of five (5) tombstoned applications beyond that or if the OS starts running low in memory the oldest tombstoned application will get terminated just like in WP7. So that is our basic deactivation pattern, to complete the lifecycle picture the user may of course choose to close the application while in the running application state by hitting the back button passed to the first page of our application. At this point, the closing event is raised and the application is terminated.

For completeness, the launching event is raised before the application event starts. So with this execution model, the typical scenario for the application that is being deactivated is for it to go to dormant state. If you’re familiar with the WP7 OS this is similar to the scenario where the user hits the starts button immediately followed by the back button to return to the application in which case no tombstoning occurs. While in Mango, deactivation without tombstoning is now the common case where in Phone 7 it used to be the exception.

One thought on “Fast Application Switching in Windows Phone

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s