This post doesn't have much code, but there's a few important points to remember for working with WinRT apps and application suspend and activation.
When your application starts, it becomes the active app. Your application will receive events that indicate your application is being started, loaded and activated.
If you've used Windows 8, you've probably noticed that you don't have any explicit way to terminate an application. (Yes, you can close an app, using the swipe action from the top to the bottom, but that doesn't actually stop the application from running.)
The environment will close your application if there's memory or CPU pressure on the machine. The first thing that will happen is that Windows will suspend your application if the system needs more resources. Your application will receive a Suspend event, and will have 5 seconds to store its state, and then the system will suspend your application.
If the system still needs more resources, the system will terminate suspended applications to free up more resources. Since your application was already suspended, your application won't get a new event for termination; rather the system will simply unload your application.
Your users have expectations around suspending, resuming, terminating, and restarting your app. When the system suspends your application, you must save state so that you can resume your app's state exactly where the user left off. You need to respond to those events, save and restore state correctly.
First, suspend: Suspend is easy. Just save your state. Do it async, and don't take more than a few seconds. App storage will backup your state into the user’s Live account storage. It will automatically be visible across Win8 machines for that user.
Next, resume: You probably don't need to do anything here. Your app was still in memory, and can simply be resumed. However, some applications, which may have time sensitive data (think news or weather apps) should look at the elapsed time and consider if they should update content. In addition, you may store state in the cloud. When you app resumes, you should consider that you may need to update the content based on the user working on your app on another machine (think EverNote). Once again, remember that the storage will be available across WIn8 machines.
Finally, consider Activated: You will get this event when your application starts. One of the properties in the event arguments is the Previous Execution State. If this is the enum value Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated, that means your application was run previously, and saved state on suspend, and was then terminated by the system to free resources. You should restore state, and use the same guidance I just gave on resume.
Of course, if you've got to code these capabilities, you need to be able to test and debug these scenarios. Visual Studio 11 gives you capabilities to do that. Unfortunately, the necessary commands are bit hidden. It took me several minutes to find the right magic to find and turn on the right commands. One of the new toolbars in VS 11 beta is the “Debug Location” toolbar. You need to customize your toolbar, and turn on this feature. It’s not on by default. The “Debug Location” toolbar (shown below) contains three new commands to suspend, resume, and suspend then terminate the app being debugged:
Once you've turned on that toolbar, simply debug your app. You'll have buttons on the toolbar button to suspend your app, or suspend and terminate your app. You can walk through the code that handles these features and ensure that your users will get the proper behavior.
All of these projects are Open Source (using the Creative Commons license for content, and the MIT license for code). If you would like to contribute, visit our GitHub Repository. Or, if you have questions, comments, or ideas for improvement, please create an issue for us.