Windows Phone 8 Application Lifecycle

Introduction

In Windows Phone you can install many apps as you desire, use them and sometimes when you use your apps you might need to switch among two or more apps. Have you ever thought what if you run all the apps at the same time or even you could be open many apps at the sametime, sometimes your phone could hang but not always and your phone battery could drain more than it should. You now why? I suppose you are interested in knowing why it does not happen.

The answer is simple, that's because the system automatically suspends and sometimes terminates the apps that are running in the background. When you launch a new app while another app is running, the first app is automatically put in a background process. It's not necessary for every app but a well-designed app can be suspended or terminated when required and re-launched by the system when required.

So, how does all these various events work? The following block diagram depicts all the various events:



As you see in the diagram above, there are the following four events:

  • Launching Event
  • Deactivated Event
  • Activated Event
  • Closing Event

The following are the two methods that exist in the process after a specific event fires:

  • OnNavigatedTo Method
  • OnNavigatedFrom Method

The following are the three possible states of a specific app when in running mode or not in running mode:

  • Running
  • Dormant
  • Tombstoned

Now let's see one by one how each event, method and state plays its role in the application lifecycle.

App Launching Event

The user taps a tile and starts the application. The application goes from the not running state to the launched state since it was started for the first time and the event handler Application_Launching is fired.

Syntax

  1. private void Application_Launching(object sender, LaunchingEventArgs e)   
  2. { }  

Since your app is being launched your app is in the running sate and it remains running until the user navigates forward, away from the app, or backwards past the app's first page. As I show in the diagram above, the OnNavigatedFrom method is called when you navigate away from one of the pages in your app.

OnNavigatedFrom Syntax

  1. protected virtual void OnNavigatedFrom(  
  2.    NavigationEventArgs e  
  3. )  

App Deactivated Event

At this point either you continue working with your app or move away from your app by pressing the Start button or by launching another application, the event handler Application_Deactivated is fired, here one more situation could exist when Application_Deactivated is fired, let's say a call comes when using an application. In these types of situtations the application goes into a dormant state and the good thing about this state is your application will automatically resume at the page where it is deactivated.

And when Application_Deactivated is fired your application should save any unsaved application data so that it can be restored at a later time, if necessary. And you need to write code for that obviously.

Syntax

  1. private void Application_Deactivated(object sender, DeactivatedEventArgs e)   
  2. { }  

Dormant to Tombstoned

When the operating system needs more memory then the OS will discard the dormant apps since Windows Phone currently can have a maximum of 6 apps in the dormant state, then it goes into the Tombstone state. The Tombstones state basically means that your app is no longer taking any system resources. All unsaved data is lost.

App Activated Event

When you return to your app and resume it from a Dormant state, the event handler Application_Activated is fired.

Syntax

  1. private void Application_Activated(object sender, ActivatedEventArgs e)   
  2. { }  

App Closing Event

When the user navigates backwards past the first page of an app and when the application is ended the Application_Closing event handler method is called.

Note: One interesting this about this event is that, suppose your app is terminated by the operating system for some reason when it is not in the foreground, the closing event will not be raised.

Syntax

  1. private void Application_Closing(object sender, ClosingEventArgs e)   
  2. { }  
Thanks I hope you like it.