Windows Phone 7 : Application Life Cycle

Given that “there can be only one” application running at a ones time, it makes creating multitasking on mobile device can be a real hard-ass, the problems is  there are many ways the user can enter and exit the software. Navigating through application is something that exists also across the applications and when the user navigates he can enter the previous application if he follows the back button stack. Also, there can be a number of events that can impact the life cycle of the program. You have also to be aware that there is actions that suspend the software and other actions that simply close it definitively.  For example when user is playing a game on her/his phone when suddenly there is incoming call to her/his phone, higher priority of incoming call make the games halt for a while, but when user has ended with the call, they want to continue the game from the last time he/she was playing it, instead restart the game from begining.

Execution Model on windows phone has a complete cycle for us to manage application state. This execution model is designed to provide user with fast and responsive experiences every time user use the application. there are some term we must know before we learn about Application execution model of windows phone.

1. Tombstoning, a procedure where windows phone operating system will deactivate apps when user trigger close event or other higher priority application/service like Incoming Call make a request to start. Operating System will save state of Apps when it’s deactivated, and will be used when user restarting the application.

2.Page State, The current visual state of a single application page. If you have a page that contains controls for the user to input data, and the user navigates away from your application and then returns to it, the user expects that all of the controls in the form will have the same values as when they left. An application that manages page state sometimes sets the values of the controls on a page when it is loaded so that the user experience is of a persistent application. When the user launches a new instance of the application, the Page UI State should reflect that it is a new instance and not load the UI state from a previous instance of the application. managing page state is done by overriding OnNavigatedTo and OnNavigatedFrom function

3. Application State, Data that is used by multiple pages in an application. An example is structured data obtained from a web service. You may want to provide different views of this data on different pages, but the data can be thought of as belonging to the application as a whole. A common misconception is that all application state data should be stored only when the application exits, but it is often a good idea to save this type of data when the user navigates away from a page. This helps to reduce the amount of state management that must be completed while the application is shutting down. Application State is managed using PhoneApplicationService class.

4. Persistent Data, Data that is used together by some application, this data is stored and resurrected in isolated storage, Application setting was one of persistent data.

5. Transient State, Data that used by initiation of an application. this data is stored on state dictionaries, which provided by PhoneApplicationService.  an Tombstoned application will reverse state of transient data when application is re-activated.

Windows Phone 7 Application Life Cycle

The following image illustrates the lifecycle of a Windows Phone application. In this diagram, the circles are application states. The rectangles show either application- or page-level events where applications should manage their state.

Execution Model Diagram for Windows Phone 7.5

The Launching Event

The Launching event is raised when a new application instance is launched by the user from the installed applications list or from a Tile on Start in addition to other means, such as tapping on a toast notification associated with an application or selecting an application from the Photos Extras menu. When an application is launched this way, it should appear to the user to be a new instance, not a continuation of a previous instance. To help ensure that your application loads quickly, you should execute as little code as possible in the handler for this event. In particular, avoid resource-intensive tasks like file and network operations. You should perform these tasks on a background thread after your application has loaded for the best user experience.

Running

After being launched, an application is running. It continues to run until the user navigates forward, away from the application, or backwards past the first application page. Windows Phone applications should not provide a mechanism for the user to quit or exit. Applications also leave the Running state when the phone’s lock screen engages unless you have disabled application idle detection.

The OnNavigatedFrom Method

The OnNavigatedFrom(NavigationEventArgs) method is called whenever the user navigates away from an application page. This can happen as the result of normal page navigation within your application. It also is called when the application is deactivated, which is discussed in the next section. Whenever this method is called, your application should store the page state so that it can be restored if the user returns to the page. The exception to this is backward navigation. The NavigationMode property can be used to determine if the navigation is a backward navigation, in which case there is no need to save state because the page will be re-created the next time it is visited.

The Deactivated Event

The Deactivated event is raised when the user navigates forward, away from your application, by pressing theStart button or by launching another application. The Deactivated event is also raised if your application launches a Chooser. For more information about Choosers.

In the handler for the Deactivated event, your application should save any application state so that it could be restored at a later time. Windows Phone applications are provided with the State object, which is a dictionary you can use to store application state. If the application is tombstoned and then reactivated, this state dictionary will be populated with the data you saved in Deactivated. Because this data is present in memory, you can use it to restore state without resource-intensive file operations.

It is possible for an application to be completely terminated after Deactivated is called. When an application is terminated, its state dictionary is not preserved. For this reason, you should also store any unsaved state that should be persisted across application instances to isolated storage during the Deactivated event.

Dormant

When the user navigates forward, away from an application, after the Deactivated event is raised, the operating system will attempt to put the application into a dormant state. In this state, all of the application’s threads are stopped and no processing takes place, but the application remains intact in memory. If the application is reactivated from this state, the application does not need to re-create any state, because it has been preserved.

If new applications are launched after an application has been made dormant, and these applications requires more memory than is available to provide a good user experience, the operating system will begin to tombstone dormant applications to free up memory.

Tombstoned

A tombstoned application has been terminated, but information about its navigation state and state dictionaries populated by the application during Deactivated are preserved. The device will maintain tombstoning information for up to five applications at a time. If an application is tombstoned and the user navigates back to the application, it will be relaunched and the application can use the preserved data to restore state. Otherwise, the application is simply terminated.

The Activated Event

The Activated event is called when the user returns to a dormant or tombstoned application. Applications should check the IsApplicationInstancePreserved property of the event args to determine whether the application is returning from being dormant or tombstoned. If IsApplicationInstancePreserved is true, then the application was dormant and state was automatically preserved by the operating system. If it is false, then the application was tombstoned and the application should use the state dictionary to restore application state. Applications should not perform resource-intensive tasks such as loading from isolated storage or a network resource during the Activated event handler because it increase the time it takes for the application to resume. Instead, these operations should be performed on a background thread after the application has loaded.

The OnNavigatedTo Method

The OnNavigatedTo(NavigationEventArgs) method is called when the user navigates to a page. This includes when the application is first launched, when the user navigates between the pages of the application, and when the application is relaunched after being made dormant or tombstoned. In this method, applications should check to see whether the page is a new instance. If it is not, state does not need to be restored. If the page is a new instance, and there is data in the state dictionary for the page, then the data should be used to restore the state of the page’s UI.

The Closing Event

The Closing event is raised when the user navigates backwards past the first page of an application. In this case, the application is terminated and no state is saved. In the Closing event handler, your application can save data that should persist across instances. There is a limit of 10 seconds for applications to complete all application and page navigation events. If this limit is exceeded, the application is terminated. For this reason, it is a good idea to save persistent state throughout the lifetime of the application and avoid having to do large amounts of file I/O in the Closing event handler.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Comment moderation is enabled. Your comment may take some time to appear.