.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Articles >> Windows Phone >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Windows Phone Application Life Cycle

Posted By:Mohhaumed Safiq SA       Posted Date: October 05, 2013    Points: 200    Category: Windows Phone    URL: http://www.dotnetspark.com  

This article gives you an detailed notes about Windows Phone Application Life Cycle

This article primarily focus on the detailed explanation about the Windows Phone Application Lifecycle.

Applies to: Windows Phone 8 | Windows Phone OS 7.1 Devices

Possible Scenarios/Navigation in Windows Phone Application:

  •     User may start the app fromstart icon or may resume the suspended app.
  •     User may close the app.
  •     User may switch over to someother app.

If you explore the App.xaml.cs, you can find four predefined event handler such as

  1.     private void Application_Launching(object sender, LaunchingEventArgs e){}
  2.     private void Application_Activated(object sender, ActivatedEventArgs e){}
  3.     private void Application_Deactivated(object sender, DeactivatedEventArgs e){}
  4.     private void Application_Closing(object sender, ClosingEventArgs e){}

These event handlers will get fired when app enters into various state. The states are Running, Not Running, Dormant, and Tombstone.

Launching and Closing the App:

    When the app isstarted for the first time, the event handler Application_Launching is fired. This won't get fired when the app resumes from thesuspended state.
    When the appis closed, the event handler Application_Closing is fired.


 The debugger will keep running even when the application is stopped running.

Deactivation and Reactivation of the app:

  •     There is adifference between Windows App and Windows Phone App. In Windows you canrun more than one app in the foreground, whereas you can run only one app in the foreground forWindows Phone. This is because for long lasting battery power and for bestuser experience.
  •     The user maymake your application dormant (alive,but not active) at any time by launching another app from the start menu.
  •     When the appis dormant, the event handler Application_Deactivated is fired.


External events such as incoming call, lock screen or any other activities keeps your app in dormant state.

  •     The user mayreturn to your app and resume it on any time. During this event, the eventhandler Application_Activated is fired.

So how to deal with Dormant?

  •     Do data persistence as much as possible during thisstate.
  •     When theapplication is returned from the dormant state, it will automaticallyresume at the page where it is deactivated.
  •     The statewhere the application left during the dormant will be preserved in the memory.

Resuming an app (Fast Application Switching):

  •     When theapplication enters into the dormant state, the dormant application listwill be maintained in the history stack.


Windows Phone OS 7.1 can maintain only up to 5 dormant apps in the history stack whereas Windows Phone OS 8 can maintain up to 8 dormant apps in the history stack.

  •     The user canresume the suspended/dormant application by continuously pressing the backkey until the user reach the suspend app.
  •     The backbutton also has another behavior called "long press". This shows the listof apps that is in dormant state. The user can select the dormantapplication from the history stack to resume the suspended app directly.

From Dormant to Tombstone:

  •     If theOperating system need more memory, the OS will discard the dormant appsaccording to their need. This process is called as "Tomb stoning".
  •      If theapplication is resumed from the dormant state, the applicationautomatically preserved and is starts from the page where it left off.
  •     When theapplication returns from the dormant state, it starts from the page whereit left off but the state will be cleared. In order to overcome thischallenge, the developer can use the ApplicationState Dictionary (ASD) which is used to save the state. The tombstoneapp will use this state to restore its data.

So how will you find the app returns dormant or tombstone?

·         You can check from the following code.

private voidApplication_Activated(object sender, ActivatedEventArgs e)
                // Dormant - state ispreserved in memory
                // Tombstone - state is cleared. Soneed to restore from ASD

  •     If theIsApplicationInstancePreserved in true, then the app returns from dormantstate. Else it returns from Tombstone state.
  •     You can usethe Application State Dictionary (ASD) by using the flowing code

 PhoneApplicationService.Current.State["Url"]= "www.msdn.com";

  •     You can saveany kind of object in this PhoneAppliationService.Current.State.
  •     So if youwant to restore the ASD data, you can use the following code.

 string url =PhoneApplicationService.Current.State["Url"].ToString();

Windows Phone Idle Detection:

  •     The Windowsphone OS can able to detect when an application is idle, and if sodetected, the OS will enable the Lock Screen.
  •     When the Lockscreen is engaged, the event handler Application_Deactivated is fired andwhen the phone is unlocked the event handler Application_Activated isfired.
  •     We can alsomake the Idle Detection to OFF state so that our app even runs whenthe lock screen is enabled.
  •     The developercan disable the Idle Detector from the following code.

 PhoneApplicationService.Current.ApplicationIdleDetectionMode= IdleDetectionMode.Disabled;


(Or) can enable from the following code


 PhoneApplicationService.Current.ApplicationIdleDetectionMode= IdleDetectionMode.Enabled;

Fast Application Resume:

  • In Windows Phone OS 7.1, a fresh instance of the app is always launched when the user starts a new copy of the app which will make to reload all the classes and data which slowdowns the app activation.
  •     In order toovercome this challenge, the Windows Phone 8 introduces a concept calledFast Application Resume (FAR) which allows you to resume the dormant appif the user starts a new copy of the app.
  •     To enable theFAR property, include the following code in the WMAppmanifest.xml

      <DefaultTaskName="_default" NavigationPage="LoginPage.xaml"ActivationPolicy="Resume"/>

  •     FAR cannot beused in every scenarios. It should be used based on the User experience onNavigation.
  •     On non-FARenabled apps, if you launch an app to Page3, then Page3 will be the onlypage in back stack and when the user press the back key, the app exits.
  •     OnFAR-enabled apps, if you launch an app to Page3, and if the suspended appbackstack contains MainPage, Page1, Page2, Page3, Page4, then the newlylaunched app will end up with the backstack of MainPage, Page1, Page2,Page3, Page4, and Page3. So be careful while choosing your scenario toenable FAR.

 Happy Reading :)

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend