Friday, 4 April 2014

Life cycle of windows phone 8 application.

       In this article we are going to see the life cycle of windows phone 8 application , for that we have to understand the application states clearly and the events fired in the page before and after that states.

       In windows phone you can see that only one app that is running in the foreground actively because that makes the resource easily access and available, allows the user use the app with responsive.

Whenever the user switch away from the application it goes to the Suspended or terminated state based on the way in which you are navigating from the app. The app's activation and deactivation are handled by the set of events.

     If you see the upper layer of the windows phone life cycle, Life cycle starts at the launching the app, after launching the app, it goes to the running state, Application leaves the running state when one of the following action occurs.

1. when user navigates to another app.
2. when user makes the phone lock.

       When user click the start button and launches the another app, now the first app goes to the Dormant state in which the app goes to the background in deactivation state,in this dormant state all application threads are stopped, but the application still intact in memory.if the application is re-activated from the dormant ,it doesn't need to do anything to re-establish the state because it is preserved.

If new applications launched then the old app are in dormant state, suddenly if the launched app needed memory to process the good responsive to the user then it need more memory, so operating system moves the app's which are present in the dormant state to the tombstone state and free the memory for running app.

       A tombstoned app are all terminated. but the operating system preserves the values of the tombstoned app's 
1. Navigate state 
2. Dictionaries state at the time of deactivation.

Operating system can store the 5 app's tombstone's information. If user navigates back to the app when the app are in tombstone then the app retain its states with preserved value.


Life cycle states with events diagram flow:
   The phone app have a life cycle which have three states running, dormant , tombstone when the app goes to this state it fires many event to reach this state. 

Events present in the life cycle:
1.  Launching
2.  Activated Event
3.  DeActivated Event
4. Closing Event

Launching : 
     when the first time user tapping the tile icon and starts the application launching event fires.

Activated Event :
     This event occurs when users returning the app from the Dormant or Tombstoned state. User have to check the property in eventargs IsApplicationInstancePreserved , when this property true then it is from the dormant state so there is no need of restore the application state operating system itself take care that. If the property is false then app is returning from the tombstoned state then user have to restore the value from the state dictionary. Large time consuming restoring process are should be done in threading.

De Activated Event :
  This event fires when the user navigates away from the app or launching a another app or launch a chooser current app goes to the de activated state , user can store the state of the app in this event and retain in when again the app is relaunched. there is a chance that app is terminated the state dictionary values can clear so in that situation user can stores the values in the isolatedstorage and retain it.

Closing Event :
  This event fires when the user navigates backwards from the first page , so there is no state value in the dictionary. This event will not fires when the app is terminated by operating system when the app is in background.

Methods present in the life cycle
1. OnNavigatedTo
2. OnNavigatedFrom

OnNavigatedTo :
    This method is called whenever user navigates to the page, following are the scenario when this method fires.

1. When user normally navigates between the pages with in the application.
2. When the application launches first time.
3. re-launching from the Tombstone or dormant state.

if the page is not a new instance then there is no need of restore the state of a page .
if the page is a new instance then use the state dictionary of that page to re-store the state of the page

OnNavigatedFrom :
   This method is fires when the app is navigated from the app page, following are the scenario when this method fires.

1. When user normally navigates between the pages with in the application.
2. When user navigates away from the your app and launches a new app.

Whenever this method is called user have to store the state of the page, so when ever user returns the page it is restored when the page is no longer in memory.But is not need for the backward navigation so this restore can be implemented by check the navigationmode property in which it will tells to the user it is navigationmode is because of backward or not. Because in Backward navigation the user don't need to restore the state it is recreated the page.

Sometimes user have to save the mediaelement state then it can be save in onnavigating method, so they can retain the state of the media element when they visit again.



I hope that from this article you can learn the windows phone application life cycle.

No comments:

Post a Comment