Windows 8 Development - Application Suspend and Shutdown
11 June 2014
At any given time, there are only three states that your Windows Store application can be in. Obviously, the first two are “running” and “not running.” But, there is a third state that the web and WinForms/WPF developer might not be accustomed to called “suspended.” An application can be suspended in several conditions:
- The user switches away from your application
- The device enters a low power state
- The developer programmatically suspends
The important thing to note as a developer is that you don't have to write any extra code to make it look as though it had been running the entire time. When your application is suspended, it remains in memory so that the user can quickly switch between suspended applications. Therefore, when it is resumed, the application picks up exactly where it left off.
As you can see in the diagram above, an application does not go directly from “running” to “not running.” It first goes through the “suspended” state. The application is notified about being suspended, but (according to a Microsoft tutorial) it is **not* notified about being terminated*. This means that every time your application is suspended, you should treat it as if your application is going to be terminated and store the user's current state so that it can be loaded again later.
Windows can terminate any suspended application at any time. Here are some conditions and rationals for why an application might be terminated:
- Windows needs to free up memory
- Windows needs to save power and/or deal with a low power situation
- When the user closes the application by pressing Alt+F4 or uses the close gesture, the application is suspended for 10 seconds and then terminated
For mobile applications, it is a best practice for user experience to return the user back to their last state in your application when they come back to it. The exceptions to this are situations where the user manually shutdown the application or where there was previously an unhandled exception. WinRT already does this for you automatically during the suspend state transition. But, it does not do this during termination. As a developer, it is your responsibility to restore terminated applications. Since there is no notification to your code that the application is being terminated, you need to save state when your application is being suspended.
WinJS also has a notification of when an application is being suspended, but it is handled by the oddly named checkpoint. This naming could make sense if there was more than one type of checkpoint, but I was unable to find any other use than suspending the app in the Microsoft documentation. Also, according to the checkpoint documentation, it appears that the "type" property on the event arguments is always "checkpoint." Out of uncertainty and no added value, I recommend using the WinRT event instead.
WinJS also has an unload event, which claims to fire when "the application is about to be unloaded." I was not able to find any other documentation around it. Also, that statement, if read in a certain way, directly contradicts the earlier statement about your application not getting notifications about application termination. Again, out of uncertainty and no added value, I recommend using the WinRT suspending event for anything you might need to do when your application is being terminated.
is licensed under a Creative Commons Attribution 4.0 International License