How to suspend an app (XAML)
Learn how to save important application data when the system suspends your app. The example registers an event handler for the Suspending event and saves a string to a file.
Roadmap: How does this topic relate to others? See:
- Roadmap for Windows Runtime apps using C# or Visual Basic
- Roadmap for Windows Runtime apps using C++
Instructions
Step 1: Register the suspending event handler
Register to handle the Suspending event, which indicates that your app should save its application data before the system suspends it.
Public NotInheritable Class MainPage Public Sub New() InitializeComponent() AddHandler Application.Current.Suspending, AddressOf App_Suspending End Sub End Class
Step 2: Save application data before suspension
When your app handles the Suspending event, it has the opportunity to save its important application data in the handler function. The app should use the LocalSettings storage API to save simple application data synchronously.
Public NonInheritable Class MainPage Private Sub App_Suspending( sender As Object, e As Windows.ApplicationModel.SuspendingEventArgs) Handles OnSuspendEvent.Suspending ' TODO: This is the time to save app data in case the process is terminated End Sub End Class
Step 3: Release exclusive resources and file handles
When your app handles the Suspending event, it also has the opportunity to release exclusive resources and file handles. Examples of exclusive resources are cameras, I/O devices, external devices, and network resources. Explicitly releasing exclusive resources and file handles helps to ensure that other apps can access them while your app isn't using them. When the app is activated after termination, it should open its exclusive resources and file handles.
Remarks
The system suspends your app whenever the user switches to another app or to the desktop or Start screen. The system resumes your app whenever the user switches back to it. When the system resumes your app, the content of your variables and data structures is the same as it was before the system suspended the app. The system restores the app exactly where it left off, so that it appears to the user as if it's been running in the background.
The system attempts to keep your app and its data in memory while it's suspended. However, if the system does not have the resources to keep your app in memory, the system will terminate your app. When the user switches back to a suspended app that has been terminated, the system sends an Activated event and should restore its application data in its OnLaunched method.
The system doesn't notify an app when it's terminated, so your app must save its application data and release exclusive resources and file handles when it's suspended, and restore them when the app is activated after termination.
A note about debugging using Visual Studio: Visual Studio prevents Windows from suspending an app that is attached to the debugger. This is to allow the user to view the Visual Studio debug UI while the app is running. When you're debugging an app, you can send it a suspend event using Visual Studio. Make sure the Debug Location toolbar is being shown, then click the Suspend icon.
Related topics
- Tasks
- How to activate an app
- How to resume an app
- Conceptual
- Application lifecycle
- Guidelines
- Guidelines for app suspend and resume
- Reference
- Windows.UI.Xaml.Application