(I have downloaded the above image from http://www.ishopping.pk/product_images/r/697/nokia-lumia-white__71613.jpg, if anybody has issue with it please mail me to firstname.lastname@example.org)
Hello guys I hope you have been enjoying your life. Now lets get down to some serious business 😉 just kidding….
There is no serious in Windows Phone. Everything goes fluently and smoothly around. 🙂
Lets start with the Windows Phone application life cycle :
Now basically in the application life cycle there are 9 steps that we need to be considered or rather we can call them as events that we need to put some light on.
- Launch Event
- OnNavigatedFrom Method
- Deactivated Event
- Activated Event
- The OnNavigatedTo Method
- The Closing Event
Now before I explain any further let me show you some life cycle diagram that I have taken from Microsoft site.
(I have downloaded the above image from Microsoft Site, if anybody has issue with it please mail me to email@example.com)
As you can see all the stages that I have mentioned above are in the diagram too. Let me tell you one thing, if you know how the application flows from start till end you will be able to understand all the Windows Phone very easily and quickly.
Lets go through each stage one by one and lets get it explained:
The Launch Event: Now as the name suggests this event launches the new instance (instance means running the application from the start, that is having a fresh start of the app) of the application. The application can be launched by selecting from the list of application or if you have a tile of the app on the home screen or from a notification that appears on the screen, which will directly take you to the app home screen. As I have mentioned that the app should look fresh and new. There should not be any reference from the previously run of the app.When you run the app the Launch event gets raised. As the launching of the app is totally dependent on this event, and as we are here to give the perfect User experience, definitely we will not be doing any heave coding here that is in the handler of this event. Whatever you want to have the heavy loading that should be done in a background thread.
Running: Well as the name says, this event is called when the app launches. Until and unless the user moves ahead or goes behind this event keeps running.Windows Phone apps shouldn’t provide a mechanism for the user to quit or exit. There is another scenario where the running stage stops, that is when the phones lock screen has been engaged. Although this can also be skipped if you have handled the application ideal detection, now in short I will try and explain the application ideal detection, this has been introduced so that the power consumption of the app can be controlled and we can make the app most power efficient. For more details follow this.
The OnNavigatedFrom: Now this method is called whenever the user navigated from a page to nay other page. This may happen in the app or outside the app, that is within your app or when the user navigates away from the app. The important point here is you should store the information or you should save the page state as when the user returns to the app again and the page is no longer in the memory. In this there is one more property called as NavigationMode property which will determine weather you page is a backward navigation or not, if it is then you should not save the state as the next visit it will recreate itself.
The Deactivated Event: This event is called when the user moves forward from the application, and this can be done either by pressing the start button on the phone or by opening another application. This event is also called when you have a chooser in place where, to get more information about it press here. Also is called if the device lock screen is enabled, unless application idle detection is disabled. Ideally speaking you should save all the data in the handler of this event so when ever you return, the data is not lost. The app is provided with the State object. It is a dictionary where you can store the app state information.
Dormant: After the deactivated state the operating system will try and put the app in the dormant state. Now the beauty of this state is that, the application processing and the threads will be stopped, that is there will be no processing done for the app but still the app remains intact in the memory. The best thing is that of the app is reactivated from this state it does not need to re-establish the state as it is all preserved 🙂 🙂 ….. Isn’t this amazing one !! . But now if the user gets on opening new apps and there is not enough memory to run the app then the operating system will push the dormant state apps in the Tombstoned state to free the memory.
Tombstoned: Now this state is really interesting one, in this state the app is terminated, its just that the operating system will save the information of the navigation state and the dictionaries that were populated during the deactivated event. The tombstoning information is saved for five apps. If the user navigates back to the tombstoned application it will take that preserved information to restore state.
The Activated Event: The activated event is called when the app returns from the Dormant or Tombstoned state. You can check that in IsApplicationInstancePreserved property where you will see if its returning from the Dormant state or Tombstoned state. If its true its Dormant, and false then Tombstoned. Again as stated above that we have to give the best User Experience so we should not do any resource intensive tasks (loading from isolated storage or a network resource) in the activated event as it will consume time for resuming the app. instead have a background thread that will do all when the app resumes.
The OnNavigatedTo Method: This method is called when the user navigates to the page. This includes when the user first launches the app or in between the app pages. This is used to check whether the page is the new instance or the one returned from Dormant or Tombstoned state. If its not a new instance naturally the page state does not need to be restored, but it is a new one then you should use the state dictionary to restore the state of the page UI.
The Closing Event: This event is called when the user navigated backward from the main/home page of the app. This time the app is terminated and no state is saved. In this event handler you should save the persistent data that you will need through out the instances of the app. The operating system gives a limited amount of 10 secs to complete all the navigation page events, as the time exceeds the app terminates.
So guys I guess you understood all the things that I have explained in the above article. Really guys if you have not understood anything out of it, please let me know so that I can help you understand it. Anything at all.
I will keep you posted more and more.
Have a happy life and keep good health … 🙂 🙂 🙂