Understanding lifecycle of Windows 8 apps
One major differentiating factor for Metro applications on Windows 8 is that the application lifetime would be managed by the OS. To the end user, this would mean that they would be unable to close applications using a close button. This has also been discussed here .
Let us now understand how the OS handles the application lifetime and what are the implying considerations for Windows 8 development. An application can be in one of the 3 states - Running, Suspended and Terminated. When an application is brought in the foreground, it becomes a running application. If second application is launched, the first one moves to the suspended state after a short delay. Again, if a third application is launched, both first and second applications move to the suspended state. This repeats until the system becomes low on memory. When the system becomes low on memory, the applications consuming maximum memory is terminated. Applications can also get terminated due to system shutdown, application crash or if the user terminates it from the task manager.
More than one application can be in the running state if they are snapped. The system resources are available to running or foreground application. When the application moves to Suspended state, it gets notified through suspend and restore events. The application gets 5 seconds to act on the notification and save its state. Once an application is suspended, it does not get access to CPU, network, hard disk and other system resources. However, the application still remains in memory. It gets resumed instantaneously from the suspended state. Thus the suspended state enables instant switching between applications. When the application is resumed from the suspended state, the application maintains its state automatically. However, by handling the restore event, tasks such as refreshing the screens with the latest data etc. can be carried out.
As seen in the screenshot above,the task manager of a windows 8 machine shows more than one application in the suspended state. While the application still consumes memory in the suspended state, the CPU utilization is 0%.
Unlike the suspended state, the applications do not get any notifications on being terminated. Therefore, applications do not get a chance to save their state before being terminated. Even on being suspended apps get only 5 seconds to save their states. Hence it is a recommended that applications should keep saving the data incrementally when the application is running. When an application is restarted after being terminated, it should be a seamless experience for the user. Hence application state when it was last launched should be persisted on the suspended event. It should be read on the activation event and the application should be launched accordingly.
As seen above the lifecycle of Windows apps is different from traditional and desktop windows applications. It provides users the ability to instantly switch between applications and gives them a better user experience. This does look promising for users with tablets and mobile devices. But might be overkill for the desktop users!