Windows App SDK app lifecycle

This article provides an overview of managing the lifecycle of Windows App SDK desktop apps.

App lifecycle overview

The application lifecycle of a Windows App SDK app is not that same as a UWP app. The lifecycle of Windows App SDK apps is similar to other .NET and Win32 desktop apps. Windows App SDK apps, like UWP apps, are started and stopped. They are either running or not running. However, unlike UWP apps, they cannot be suspended and resumed. At the window level, your app can subscribe to events to react when windows are activated and deactivated.

Microsoft.UI.Xaml.Application lifecycle

The Application object is the main entry point for a Windows App SDK app. It's similar to the UWP Application class, but with some important differences. The Application object is created by the Windows App SDK framework and is accessible from the Microsoft.UI.Xaml.Application.Current property.

The Application class in Windows App SDK has only one lifecycle method, OnLaunched, which is called when the app is launched. The OnLaunched method is responsible for creating the app's main window and displaying it. The OnLaunched method is also responsible for initializing the Windows App SDK framework and starting the app. When you create a new Windows App SDK app, the OnLaunched method is automatically generated for you.

In contrast, the UWP Application class has several activation-related lifecycle methods, including OnLaunched, OnActivated, and OnBackgroundActivated. The OnActivated and OnBackgroundActivated methods are called when the app is activated. The OnActivated method is called when the app is activated by the user, and the OnBackgroundActivated method is called when the app is activated by the system.

UWP's Application class also has several lifecycle events: Suspending, Resuming, EnteredBackground, and LeavingBackground. The Suspending event is raised when the app is suspended, and the Resuming event is raised when the app is resumed. The EnteredBackground event is raised when the app enters the background, and the LeavingBackground event is raised when the app leaves the background. For a full explanation of UWP lifecycle events, see Windows 10 UWP app lifecycle.

If you are migrating a UWP app to Windows App SDK, you can use the Application lifecycle functionality migration guide to understand the differences between the UWP and Windows App SDK app lifecycles.

Microsoft.UI.Xaml.Window lifecycle

The Window object in Windows App SDK has some lifecycle events as well, Window.Activated and Window.Closed.

Window.Activated

The Activated event is raised when the window has been activated or deactivated by the system. Apps can determine what the status of the Window activation is by checking the WindowActivationState property of the WindowActivatedEventArgs parameter. This event will fire any time the window is activated or deactivated, including when the window is minimized or maximized.

Window.Closed

The Closed event is raised when the window closes. If this is the last window to be closed, usually the app's MainWindow, the application will be terminated. Because there is no Suspending event raised by the Application object in Windows App SDK, you should use your main window's Closed event to save application state and clean up any managed resources.

See also

App lifecycle and system services

UWP app lifecycle