12 - Managing Windows App Life cycle

Window store apps can be used on a variety of devices those are differ in their processing and power backup capabilities, so it is mandatory to save the state of an app, which ensures optimum use of system resources.

In window store apps, it is to keep in focus that the app running in foreground should acquire all the resources. The apps, those are running in background are suspended or terminated if the resources are not available. While a user switched back to a suspended or terminated app, it should start from the last state of the app. To ensure that the app should not lost its state if it is suspended or terminated, a developer should implement the state of the app.

App Lifecycle

When a window store app is deployed, it may present in any one of the following phase throughout its life cycle:

Running: This is the app that is running. It is in the foreground.

Suspended: An app enters in suspending state, if is minimized or lost focus. It is in background.

Not Running: An app that is terminated by operating system due to lack of resources, is in terminated state. When we press Alt+F4 key, app is terminated.

Application Events

An application event is raised by an app during its lifecycle. We can define event handlers for various events. Application events are defined in the Application class. We can implement these events in the App.xaml.cs file of application.

Application class has the following events:

Suspending Event

This event occurs when an app is transit to suspend phase. When multiple apps are transferred to suspend phase, they all consume some resources and it may produce low system resources problem. So, operating system will terminate all these apps to release the system resources to improve the system performance. The Suspending event is then useful to save the state of app. The syntax of this event is as follows:

Here e is the instance of SuspendingEventArgs class. This method is static so that it can be executed with the help of reference of App class.

While implementing this event handler in app we need to:

Define the actions those should be performed

Here we have to mention all those actions that are useful to save the state of app.

Delay the suspension process

This event has only 10 seconds to complete its operation. So, to ensure that all the steps are executed we need to delay the suspension process. For this GetDeferral() method is used in the following way:

This Event is now need to be associated with the method. For this the constructor of the App class can be used as:

Resuming Event

This event occurs when user switches back to a suspended app. At this moment the state of app is automatically restored. For this process OnLaunched() method is used that is defined in App class. While using this event in app we need to:

Define the event handler

Following code can be used to define resuming event handler in app:

Void App_Resuming(object sender, object e) { }

Associate the event handler with the Resuming event.

After the resuming event handler is defined, it needs to be associated with the Resuming event of the app. For this we have to write following code in the constructor of App class:

this.Resuming+=App_Resuming;

UnhandledException Event

This is an Exception occurs in case of unhandled exception in app. After this exception app is terminated. To use this exception in app we need to:

Define the event handler

Here we can define the code that will not terminate the app abruptly, it will show user defined message to the end user. For this we can use following code:

After defining the event handler, it needs to be associated with the UnhandledException event of the app. For this following code is to be written in the constructor of App class.

State management in Window Store App

There can be of two types of data that should be saved in an app:

App data

The data that should be persisting across the sessions is called App data. Like in weather app a user can set the preference about the temperature that is should be in Celsius or Fahrenheit. In case of App data the lifetime of app is very important; it will not persist if the app is uninstalled from the device.

Session data

Data that is related to a particular session only is called session data. A session ends if user closes the app, reboot the system or logs off the system. But in case of suspending the app, if user switches back to the app same session supposed to continue.

To implement state management in an app we need to plan the requirements of our app and then we should write the code to implement the storage and restoration of state.

There are some specific points that should be considered for this:

If an app is suspended for a long period of time, it should always start afresh,

For this we can define time duration.

If there is need to maintain the state of app and enable its restoration, always save the state when it is suspended.

When app is resumed, there should be a confirmation about to continue with the last state of app or start afresh

When app is suspended, release all the resources.

If app is terminated abnormally, always start afresh.

If app contains dynamic or frequently changing contents, there is no need to save the state.

There is no need of close button in App. User can use Alt+F4 key for that.

Saving App data

Sample: To save the data of TextBox, we can use the following code in the TextChanged event of TextBox.

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["Name"] = TextBox1.Text;
In the preceding code LocalSettings is the container which is used to store the contents on local machine at a specific location. We can also use RoamingSettings to store data for accessing the same app across the devices.
There may be three folders to save data:
Local folder
Roaming folder
Temporary folder

Restoring App data

The settings stored in LocalSettings container can be restored when the app is launched next time. For this we have to apply actions in OnLaunched() method.