Published: 09 Feb 2012
By: Dhananjay Kumar

An overview of fast application switching and page state in Windows Phone.

Contents [hide]

Introduction

Windows Phone 7.5 operating system provides improved fast application switching over Windows Phone 7 operating system. Before we go into details of how FAS is better in Mango, let us first understand what is FAS.

Fast Application Switching

Imagine application "A" is running and user launches a new application "B". On pressing hardware back button user can return back to application "A". If application "A" gets activated without "resuming " screen then it can be termed as " Fast Application Switching "

In Windows Phone 7.5 version FAS is improved over Windows Phone 7 with introduction of new state in application life cycle called "Dormant state". Whereas in Windows Phone 7.0 version there was no Dormant state and on deactivation application directly goes to Tombstone state.

In Windows Phone 7 flow of application was as below,

  • User deactivates application by pressing hardware start or search button
  • Application goes to tombstone state
  • Windows Phone 7 operating system saves information about application Navigation state and State Dictionary
  • Operating system terminates application thread and processes.
  • User again activates application by pressing hardware back button.
  • Operating system creates application thread and process.
  • Operating system restores memory state by retrieving saved value of Navigation state and state dictionary.

Due to above steps in Windows Phone 7 operating system user gets feeling that application was running in background and got resumed from there. However this is not true and operating system again creates application process and restores memory state. Creation of application process and restoration of application memory states take time and cause to slower application switching.

In Windows Phone 7.5 flow of application is as below,

  • User deactivates application by pressing hardware start or search button
  • Application goes to dormant state
  • Windows Phone 7.5 operating system preserve instance of the application
  • User again activates application by pressing hardware back button.
  • Operating system restores preserve instance and re activate application
  • If operating system runs low on the memory, it pushes application from dormant state to tombstone state.
  • If application is reactivating from tombstone state then it follows the same cycle as of Windows Phone 7 operation system.

Now you understand significant of dormant state for Fast Application switch. With introduction of dormant state in application life cycle, operating system preserve instance of the application in memory unless it is forced to release due to low memory. So at the time of application reactivation as a developer you need to check whether to restore the application or not? If application is reactivating from dormant state then there should not be restoration.

On Application Activated event you can check whether application is reactivating from dormant or tombstone state as below,

An Application gets closed by pressing hardware back button. And in that case application neither goes to dormant state or tombstone state. If there are multiple pages in application then user navigates between pages using hardware back button. However if on start page of application user press hardware back button then operating system will close the application. While closing application Application_Closing() events gets executed.

Figure 3: Block diagram on Application Life Cycle events

Block diagram on Application Life Cycle events

There are two types of state windows phone deals with

  • Page state
  • Application state

Page state deals with transient data whereas Application State deals with Persistent Data.

Page state deals with preserving data binding of the user interface of the page. If application is reactivating from dormant state then there is no need of preserving the state of the page or in simple words data of the controls on page explicitly. However if application is reactivating from tombstone state then page data need to be preserved and restore explicitly.

To demonstrate how Page State works? I have designed page as below,

Let us put some control on the phone application page.

There are three controls on the page. After tombstone, we need to preserve and restore value of these controls. Now we need to create properties for each control. We need to call NotifyPropertyChanged event in the setter of property to notify when value changed at the control. Control value would be bind with two ways binding with these properties. Eventually Data class will be as below,

With Data Class in place we need to preserve state of the page. There are two checks to perform before preserving page state. First check to be done, whether application is reactivating from dormant state. If so then there is no need of restoring the state. To save the state of the page OnNavigatedFrom method instance of the Data need to be saved in the State dictionary as below,

In above code dataToBindControls is object of Data bind to the controls on the page. To restore the page OnNavigatedTo method state count need to checked as below.

Essentially there are three tasks we are performing in above function

  • Checking if it is new instance of the page.
  • If yes then checking if value of data control [object of data class ] is null
  • If null then reading from the state else creating new instance of that.

Full source code behind to preserve page is as below,

Last but not least data need to be bind to controls as below,

In this way a page state can be preserved. Before debugging last step we need to perform is enable Tombstone upon deactivation while debugging in the properties page. There may be other approach to preserve state as below,

  • Creating extension method for page class
  • Create helper class and call method of the class to save control's data.

I will conclude with supporting above approach of Page State restore and retrieve for better encapsulation. Since each page is handling their own state so it is easy to separate the concern for pages individually.

In next article we will dig more into application state. I hope this article is useful.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Dhananjay Kumar

Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter @debug_mode

This author has published 8 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Create Android AngryBirds Game Using WiEngine SDK -Part 1
AngryBirds, as a strategy puzzle mobile game developed by Finnish computer game developer Rovio Mobi...
Integrate Lua into Your Android Games
In this article, we will show interests in the interoperations between Lua and Java, especially in t...
Create Android AngryBirds Game Using WiEngine SDK -Part 2
Starting from this article I'm going to introduce how to compose the main UIs in creating the AngryB...
Create Android AngryBirds Game Using WiEngine SDK -Part 3
In the second part of this series, I introduced how to create most of the gadgets in the prelude scr...
Take a Test Application in Windows Phone 7
In this article we would see how to make a Test application, what I mean is an application where a u...

You might also be interested in the following related blog posts


CodeDigest.Com - Articles,FAQs, Codes, News - Aug,2009 read more
Silverlight 3 Navigation: Dynamically Loaded Pages Now MEF Powered! read more
Updated SilverTwit Code for MSDN Magazine read more
How do ASP.NET Application_ Events Work read more
CodeDigest.Com Article,Codes,FAQs - April,2009 read more
ViewModel with MVC/Navigation in Silverlight read more
Displaying XPS Documents in Silverlight read more
Provisioning a UCMA application using ApplicationProvisioner.exe read more
Composite Application Guidance for WPF. read more
ASP.NET MVC Plan 9 Sample read more
Top
 
 
 

Please login to rate or to leave a comment.