Overview

.NET MAUI raises cross-platform lifecycle events on the Window class when the app transitions states.

State Transitions

Begin StateEventEnd State
RunningDeactivatedDeactivated
DeactivatedStoppedStopped
StoppedResumedRunning
StoppedDestoryingNot running
Not runningCreated, ActivatedRunning

Events

Note: These events have different names on Android, iOS.

EventRaised whenAction
CreatedNative window created (but perhaps not yet visible)N/A
ActivatedWindow activatedN/A
DeactivatedWindow no longer focusedN/A
StoppedWindow no longer visibleDisconnect; cancel pending requests
ResumedApp resumes from being Stopped (never raised first time app launches)Subscribe to events; refresh content that’s visible on the page
DestroyingNative window is being destroyedRemove event subscriptions attached to the native window.

Window Class Virtual Methods

These methods are invoked when the corresponding event is raised: OnCreated, OnActivated, OnDeactivated, OnStopped, OnResumed, OnDestroying

Subscribing to Window Lifecycle Events

Override the CreateWindow method in App class and create a Window instance:

public partial class App : Application 
{
    public App() 
    {
        InitializeComponent();

        MainPage = new MainPage();
    }

    protected override Window CreateWindow(IActivationState activationState) 
    {
        Window window = base.CreateWindow(activationState);

        window.Created += (s, e) => 
        {
            // Custom logic
        };

        return window;
    }
}

Consuming Window Lifecycle Events

Create a class that derives from Window:

public class MyWindow : Window 
{
    public MyWindow() : base() { }

    public MyWindow(Page page) : base(page) 
    {
        // Note: if this constructor is used and `App.MainPage` is already set, `InvalidOperationException`
        is thrown.
    }

    protected override void OnCreated() 
    {
        // Register services
    }
}

Then, override the CreateWindow method in App class and return a MyWindow instance.

Custom Lifecycle Events