MAUI Application Lifecycle

Learn the lifecycle of a .NET MAUI app.

When a native application loads, it goes through a series of steps. For example, an application window is created. Then the application is activated. At some point, the application can be backgrounded. Then it can be resumed. Then, after some time, the application is closed, and its instance is removed from the memory of the device.

.NET MAUI allows us to catch all of these events and apply custom logic to them. To demonstrate this, we have the following project setup:

<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:MauiAppLifecycle"
             x:Class="MauiAppLifecycle.App">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/Styles/Colors.xaml" />
                <ResourceDictionary Source="Resources/Styles/Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
MAUI app lifecycle demonstrated

Note: The log entries created by the event handlers in the code above would only be observable in an app running in debug mode. These event handlers don't modify any visible behavior of the app. We'll have the opportunity to practice and observe this behavior in the “Try It Yourself” lesson.

Platform-independent lifecycle events

MAUI has some platform-independent and platform-specific lifecycle events. All platform-independent events can be attached to a Window object ...