Claude Cursor GitHub Copilot Skill

maui-app-lifecycle

.NET MAUI app lifecycle guidance — the four app states, cross-platform Window lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying), platform-specific lifecycle mapping, backgrounding and resume behavior, and state-preservation patterns. USE FOR: "app l

LLM Mart · 0 points · 20 views 0 listing impressions 0 install-command copies
Virus-scanned Reviewed automatically before listing.

Full trust report

Download dotnet-skills-plugins_dotnet-maui_skills_maui-app-lifecycle-98f8485.zip · 5 KB
Part of dotnet/skills — 119 skills

Install

skills CLI npx skills add https://github.com/dotnet/skills/tree/main/plugins/dotnet-maui/skills/maui-app-lifecycle
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install dotnet-skills@llmmart
Git git clone https://github.com/dotnet/skills.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole dotnet/skills collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

.NET MAUI App Lifecycle

Handle application state transitions correctly in .NET MAUI. This skill covers the cross-platform Window lifecycle events, their platform-native mappings, and patterns for preserving state across backgrounding and resume cycles.

When to Use

  • Saving or restoring state when the app backgrounds or resumes
  • Subscribing to Window lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying)
  • Hooking into platform-native lifecycle callbacks via ConfigureLifecycleEvents
  • Deciding where to place initialization, teardown, or refresh logic
  • Understanding the difference between Deactivated and Stopped

When Not to Use

  • Page-level navigation events — use Shell navigation guidance instead
  • Registering services at startup — use dependency injection guidance instead
  • Calling platform-specific APIs outside lifecycle context — use platform invoke guidance instead

Inputs

  • The target lifecycle transition (e.g., "save draft when backgrounded", "refresh data on resume")
  • Which platforms the developer targets (Android, iOS, Mac Catalyst, Windows)
  • Whether the app uses multiple windows (iPad, Mac Catalyst, desktop Windows)

App States

A .NET MAUI app moves through four states:

State Description
Not Running Process does not exist
Running Foreground, receiving input
Deactivated Visible but lost focus (dialog, split-screen, notification shade)
Stopped Fully backgrounded, UI not visible

Typical flow: Not Running → Running → Deactivated → Stopped → Running (resumed) or Not Running (terminated).

Window Lifecycle Events

Microsoft.Maui.Controls.Window exposes six cross-platform events:

Event Fires when
Created Native window allocated
Activated Window receives input focus
Deactivated Window loses focus (may still be visible)
Stopped Window is no longer visible
Resumed Window returns to foreground after Stopped
Destroying Native window is being torn down

Subscribing via CreateWindow

Override CreateWindow in your App class and attach event handlers:

public partial class App : Application
{
    protected override Window CreateWindow(IActivationState? activationState)
    {
        var window = base.CreateWindow(activationState);

        window.Created += (s, e) => Debug.WriteLine("Created");
        window.Activated += (s, e) => Debug.WriteLine("Activated");
        window.Deactivated += (s, e) => Debug.WriteLine("Deactivated");
        window.Stopped += (s, e) => Debug.WriteLine("Stopped");
        window.Resumed += (s, e) => Debug.WriteLine("Resumed");
        window.Destroying += (s, e) => Debug.WriteLine("Destroying");

        return window;
    }
}

Subscribing via a Custom Window Subclass

Create a Window subclass and override the virtual methods:

public class AppWindow : Window
{
    public AppWindow(Page page) : base(page) { }

    protected override void OnActivated() { /* refresh UI */ }
    protected override void OnStopped() { /* save state */ }
    protected override void OnResumed() { /* restore state */ }
    protected override void OnDestroying() { /* cleanup */ }
}

Return it from CreateWindow:

protected override Window CreateWindow(IActivationState? activationState)
    => new AppWindow(new AppShell());

Workflow: Save and Restore State on Background

  1. Identify transient state — draft text, scroll position, form inputs, timer values.
  2. Save in OnStopped — use Preferences for small values or file serialization for larger state.
  3. Restore in OnResumed — read back saved values and apply to your view model.
  4. Also save in OnDestroying on Android — the back button can skip Stopped entirely.
  5. Keep handlers fast — complete within 1–2 seconds to avoid ANR on Android or watchdog kills on iOS.
protected override void OnStopped()
{
    base.OnStopped();
    Preferences.Set("draft_text", _viewModel.DraftText);
    Preferences.Set("scroll_y", _viewModel.ScrollY);
}

protected override void OnResumed()
{
    base.OnResumed();
    _viewModel.DraftText = Preferences.Get("draft_text", string.Empty);
    _viewModel.ScrollY = Preferences.Get("scroll_y", 0.0);
}

protected override void OnDestroying()
{
    base.OnDestroying();
    // Android back-button can skip Stopped
    Preferences.Set("draft_text", _viewModel.DraftText);
}

Platform Lifecycle Mapping

Android

Window Event Android Callback
Created OnCreate
Activated OnResume
Deactivated OnPause
Stopped OnStop
Resumed OnRestart → OnStart → OnResume
Destroying OnDestroy

iOS / Mac Catalyst

Window Event UIKit Callback AddiOS builder method
Created WillFinishLaunching / SceneWillConnect .WillFinishLaunching() / .SceneWillConnect()
Activated DidBecomeActive .OnActivated()
Deactivated WillResignActive .OnResignActivation()
Stopped DidEnterBackground .DidEnterBackground()
Resumed WillEnterForeground .WillEnterForeground()
Destroying WillTerminate .WillTerminate()

⚠️ The UIKit selector names and the AddiOS builder method names differ for activation. There is no .DidBecomeActive() or .WillResignActive() builder method — use .OnActivated() and .OnResignActivation() or the code will not compile.

Windows (WinUI)

Window Event WinUI Callback
Created OnLaunched
Activated Activated (foreground)
Deactivated Activated (background)
Stopped VisibilityChanged (false)
Resumed VisibilityChanged (true)
Destroying Closed

Hooking Native Lifecycle Directly

Use ConfigureLifecycleEvents in MauiProgram.cs when you need platform-specific callbacks beyond what Window events provide:

builder.ConfigureLifecycleEvents(events =>
{
#if ANDROID
    events.AddAndroid(android => android
        .OnCreate((activity, bundle) => Debug.WriteLine("Android OnCreate"))
        .OnResume(activity => Debug.WriteLine("Android OnResume"))
        .OnPause(activity => Debug.WriteLine("Android OnPause"))
        .OnStop(activity => Debug.WriteLine("Android OnStop"))
        .OnDestroy(activity => Debug.WriteLine("Android OnDestroy")));
#elif IOS || MACCATALYST
    events.AddiOS(ios => ios
        .OnActivated(app => Debug.WriteLine("iOS OnActivated"))
        .OnResignActivation(app => Debug.WriteLine("iOS OnResignActivation"))
        .DidEnterBackground(app => Debug.WriteLine("iOS DidEnterBackground"))
        .WillEnterForeground(app => Debug.WriteLine("iOS WillEnterForeground")));
#elif WINDOWS
    events.AddWindows(windows => windows
        .OnLaunched((app, args) => Debug.WriteLine("Windows OnLaunched"))
        .OnActivated((window, args) => Debug.WriteLine("Windows Activated"))
        .OnClosed((window, args) => Debug.WriteLine("Windows Closed")));
#endif
});

Common Pitfalls

  1. Resumed does not fire on first launch. The initial sequence is Created → Activated. Use OnActivated for logic that must run on every foreground entry, not OnResumed.

  2. Deactivated ≠ Stopped. A dialog, split-screen, or notification pull-down triggers Deactivated without Stopped. Do not perform heavy saves in OnDeactivated — the app may never actually background.

  3. Android back button skips Stopped. On Android, pressing back may call Destroying directly without Stopped. Place critical save logic in both OnStopped and OnDestroying.

  4. Multi-window apps fire events independently. On iPad, Mac Catalyst, and desktop Windows each Window instance fires its own lifecycle events. Do not assume a single global lifecycle.

  5. Long-running handlers cause kills. Android enforces a ~5 second ANR timeout; iOS has limited background execution time. Keep lifecycle handlers synchronous and fast — use Preferences for quick saves, not database writes.

  6. Do not use legacy Xamarin.Forms lifecycle methods. Application.OnStart(), Application.OnSleep(), and Application.OnResume() exist for backward compatibility but bypass Window-level events. In .NET MAUI, prefer Window lifecycle events (OnActivated, OnStopped, OnResumed, etc.) for correct multi-window behavior.

Files (skills)
  • references
    • lifecycle-api.md 5.5 KB
      # .NET MAUI App Lifecycle — API Reference
      
      ## App States
      
      A MAUI app moves through four logical states:
      
      | State | Meaning |
      |---|---|
      | **Not Running** | App process does not exist. |
      | **Running** | App is in the foreground and receiving input. |
      | **Deactivated** | App is visible but lost focus (e.g. a dialog or split-screen). |
      | **Stopped** | App is fully backgrounded; UI is not visible. |
      
      Typical flow: Not Running → Running → Deactivated → Stopped → Running (resumed) or Not Running (terminated).
      
      ## Cross-platform Window Events
      
      `Microsoft.Maui.Controls.Window` exposes six lifecycle events:
      
      | Event | When it fires |
      |---|---|
      | `Created` | Window has been created (native window allocated). |
      | `Activated` | Window has been activated and is receiving input. |
      | `Deactivated` | Window lost focus but may still be visible. |
      | `Stopped` | Window is no longer visible (backgrounded). |
      | `Resumed` | Window returns to the foreground after being stopped. |
      | `Destroying` | Window is being torn down (native window deallocated). |
      
      ## Subscribing to Window Events
      
      ### Option A — Override `CreateWindow` in `App`
      
      ```csharp
      public partial class App : Application
      {
          protected override Window CreateWindow(IActivationState? activationState)
          {
              var window = base.CreateWindow(activationState);
      
              window.Created += (s, e) => Log("Window Created");
              window.Activated += (s, e) => Log("Window Activated");
              window.Deactivated += (s, e) => Log("Window Deactivated");
              window.Stopped += (s, e) => Log("Window Stopped");
              window.Resumed += (s, e) => Log("Window Resumed");
              window.Destroying += (s, e) => Log("Window Destroying");
      
              return window;
          }
      }
      ```
      
      ### Option B — Custom Window subclass with overrides
      
      ```csharp
      public class AppWindow : Window
      {
          public AppWindow() : base() { }
          public AppWindow(Page page) : base(page) { }
      
          protected override void OnCreated() { /* init work */ }
          protected override void OnActivated() { /* refresh UI */ }
          protected override void OnDeactivated() { /* pause timers */ }
          protected override void OnStopped() { /* save state */ }
          protected override void OnResumed() { /* restore state */ }
          protected override void OnDestroying() { /* cleanup */ }
      }
      ```
      
      Return it from `CreateWindow`:
      
      ```csharp
      protected override Window CreateWindow(IActivationState? activationState)
      {
          return new AppWindow(new AppShell());
      }
      ```
      
      ## Platform Lifecycle Event Mapping
      
      ### Android
      
      | Window event | Android Activity callback |
      |---|---|
      | Created | `OnCreate` |
      | Activated | `OnResume` |
      | Deactivated | `OnPause` |
      | Stopped | `OnStop` |
      | Resumed | `OnRestart` → `OnStart` → `OnResume` |
      | Destroying | `OnDestroy` |
      
      ### iOS / Mac Catalyst
      
      | Window event | UIKit callback | `AddiOS` builder method |
      |---|---|---|
      | Created | `WillFinishLaunching` / `SceneWillConnect` | `.WillFinishLaunching()` / `.SceneWillConnect()` |
      | Activated | `DidBecomeActive` | `.OnActivated()` |
      | Deactivated | `WillResignActive` | `.OnResignActivation()` |
      | Stopped | `DidEnterBackground` | `.DidEnterBackground()` |
      | Resumed | `WillEnterForeground` | `.WillEnterForeground()` |
      | Destroying | `WillTerminate` | `.WillTerminate()` |
      
      > ⚠️ The activation builder methods are **not** named after the UIKit selectors.
      > `.DidBecomeActive()` and `.WillResignActive()` do not exist on `IiOSLifecycleBuilder` —
      > use `.OnActivated()` and `.OnResignActivation()`.
      
      ### Windows (WinUI)
      
      | Window event | WinUI callback |
      |---|---|
      | Created | `OnLaunched` |
      | Activated | `Activated` (foreground) |
      | Deactivated | `Activated` (background) |
      | Stopped | `VisibilityChanged` (false) |
      | Resumed | `VisibilityChanged` (true) |
      | Destroying | `Closed` |
      
      ## Platform-specific Lifecycle Events
      
      Use `ConfigureLifecycleEvents` in `MauiProgram.cs` to hook directly into native callbacks:
      
      ```csharp
      builder.ConfigureLifecycleEvents(events =>
      {
      #if ANDROID
          events.AddAndroid(android => android
              .OnCreate((activity, bundle) => Log("Android OnCreate"))
              .OnStart(activity => Log("Android OnStart"))
              .OnResume(activity => Log("Android OnResume"))
              .OnPause(activity => Log("Android OnPause"))
              .OnStop(activity => Log("Android OnStop"))
              .OnDestroy(activity => Log("Android OnDestroy")));
      #elif IOS || MACCATALYST
          events.AddiOS(ios => ios
              .WillFinishLaunching((app, options) => { Log("iOS WillFinishLaunching"); return true; })
              .SceneWillConnect((scene, session, options) => Log("iOS SceneWillConnect"))
              .OnActivated(app => Log("iOS OnActivated"))
              .OnResignActivation(app => Log("iOS OnResignActivation"))
              .DidEnterBackground(app => Log("iOS DidEnterBackground"))
              .WillTerminate(app => Log("iOS WillTerminate")));
      #elif WINDOWS
          events.AddWindows(windows => windows
              .OnLaunched((app, args) => Log("Windows OnLaunched"))
              .OnActivated((window, args) => Log("Windows Activated"))
              .OnClosed((window, args) => Log("Windows Closed")));
      #endif
      });
      ```
      
      ## State Preservation Pattern
      
      Save and restore transient state during backgrounding:
      
      ```csharp
      protected override void OnStopped()
      {
          base.OnStopped();
          Preferences.Set("draft_text", _viewModel.DraftText);
          Preferences.Set("scroll_position", _viewModel.ScrollY);
      }
      
      protected override void OnResumed()
      {
          base.OnResumed();
          _viewModel.DraftText = Preferences.Get("draft_text", string.Empty);
          _viewModel.ScrollY = Preferences.Get("scroll_position", 0.0);
      }
      ```
      
      For larger state, use `SecureStorage` or file-based serialization instead of `Preferences`.
      
  • SKILL.md 9 KB
    ---
    name: maui-app-lifecycle
    description: >-
      .NET MAUI app lifecycle guidance — the four app states, cross-platform Window
      lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying),
      platform-specific lifecycle mapping, backgrounding and resume behavior, and
      state-preservation patterns.
      USE FOR: "app lifecycle", "window lifecycle events", "save state on background",
      "resume app", "OnStopped", "OnResumed", "backgrounding", "deactivated event",
      "ConfigureLifecycleEvents", "platform lifecycle hooks".
      DO NOT USE FOR: navigation events (use maui-shell-navigation),
      dependency injection setup (use maui-dependency-injection),
      platform API invocation (use conditional compilation and partial classes).
    license: MIT
    ---
    
    # .NET MAUI App Lifecycle
    
    Handle application state transitions correctly in .NET MAUI. This skill covers the cross-platform Window lifecycle events, their platform-native mappings, and patterns for preserving state across backgrounding and resume cycles.
    
    ## When to Use
    
    - Saving or restoring state when the app backgrounds or resumes
    - Subscribing to Window lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying)
    - Hooking into platform-native lifecycle callbacks via `ConfigureLifecycleEvents`
    - Deciding where to place initialization, teardown, or refresh logic
    - Understanding the difference between Deactivated and Stopped
    
    ## When Not to Use
    
    - Page-level navigation events — use Shell navigation guidance instead
    - Registering services at startup — use dependency injection guidance instead
    - Calling platform-specific APIs outside lifecycle context — use platform invoke guidance instead
    
    ## Inputs
    
    - The target lifecycle transition (e.g., "save draft when backgrounded", "refresh data on resume")
    - Which platforms the developer targets (Android, iOS, Mac Catalyst, Windows)
    - Whether the app uses multiple windows (iPad, Mac Catalyst, desktop Windows)
    
    ## App States
    
    A .NET MAUI app moves through four states:
    
    | State | Description |
    |---|---|
    | **Not Running** | Process does not exist |
    | **Running** | Foreground, receiving input |
    | **Deactivated** | Visible but lost focus (dialog, split-screen, notification shade) |
    | **Stopped** | Fully backgrounded, UI not visible |
    
    Typical flow: Not Running → Running → Deactivated → Stopped → Running (resumed) or Not Running (terminated).
    
    ## Window Lifecycle Events
    
    `Microsoft.Maui.Controls.Window` exposes six cross-platform events:
    
    | Event | Fires when |
    |---|---|
    | `Created` | Native window allocated |
    | `Activated` | Window receives input focus |
    | `Deactivated` | Window loses focus (may still be visible) |
    | `Stopped` | Window is no longer visible |
    | `Resumed` | Window returns to foreground after Stopped |
    | `Destroying` | Native window is being torn down |
    
    ### Subscribing via CreateWindow
    
    Override `CreateWindow` in your `App` class and attach event handlers:
    
    ```csharp
    public partial class App : Application
    {
        protected override Window CreateWindow(IActivationState? activationState)
        {
            var window = base.CreateWindow(activationState);
    
            window.Created += (s, e) => Debug.WriteLine("Created");
            window.Activated += (s, e) => Debug.WriteLine("Activated");
            window.Deactivated += (s, e) => Debug.WriteLine("Deactivated");
            window.Stopped += (s, e) => Debug.WriteLine("Stopped");
            window.Resumed += (s, e) => Debug.WriteLine("Resumed");
            window.Destroying += (s, e) => Debug.WriteLine("Destroying");
    
            return window;
        }
    }
    ```
    
    ### Subscribing via a Custom Window Subclass
    
    Create a `Window` subclass and override the virtual methods:
    
    ```csharp
    public class AppWindow : Window
    {
        public AppWindow(Page page) : base(page) { }
    
        protected override void OnActivated() { /* refresh UI */ }
        protected override void OnStopped() { /* save state */ }
        protected override void OnResumed() { /* restore state */ }
        protected override void OnDestroying() { /* cleanup */ }
    }
    ```
    
    Return it from `CreateWindow`:
    
    ```csharp
    protected override Window CreateWindow(IActivationState? activationState)
        => new AppWindow(new AppShell());
    ```
    
    ## Workflow: Save and Restore State on Background
    
    1. **Identify transient state** — draft text, scroll position, form inputs, timer values.
    2. **Save in `OnStopped`** — use `Preferences` for small values or file serialization for larger state.
    3. **Restore in `OnResumed`** — read back saved values and apply to your view model.
    4. **Also save in `OnDestroying`** on Android — the back button can skip `Stopped` entirely.
    5. **Keep handlers fast** — complete within 1–2 seconds to avoid ANR on Android or watchdog kills on iOS.
    
    ```csharp
    protected override void OnStopped()
    {
        base.OnStopped();
        Preferences.Set("draft_text", _viewModel.DraftText);
        Preferences.Set("scroll_y", _viewModel.ScrollY);
    }
    
    protected override void OnResumed()
    {
        base.OnResumed();
        _viewModel.DraftText = Preferences.Get("draft_text", string.Empty);
        _viewModel.ScrollY = Preferences.Get("scroll_y", 0.0);
    }
    
    protected override void OnDestroying()
    {
        base.OnDestroying();
        // Android back-button can skip Stopped
        Preferences.Set("draft_text", _viewModel.DraftText);
    }
    ```
    
    ## Platform Lifecycle Mapping
    
    ### Android
    
    | Window Event | Android Callback |
    |---|---|
    | Created | `OnCreate` |
    | Activated | `OnResume` |
    | Deactivated | `OnPause` |
    | Stopped | `OnStop` |
    | Resumed | `OnRestart` → `OnStart` → `OnResume` |
    | Destroying | `OnDestroy` |
    
    ### iOS / Mac Catalyst
    
    | Window Event | UIKit Callback | `AddiOS` builder method |
    |---|---|---|
    | Created | `WillFinishLaunching` / `SceneWillConnect` | `.WillFinishLaunching()` / `.SceneWillConnect()` |
    | Activated | `DidBecomeActive` | `.OnActivated()` |
    | Deactivated | `WillResignActive` | `.OnResignActivation()` |
    | Stopped | `DidEnterBackground` | `.DidEnterBackground()` |
    | Resumed | `WillEnterForeground` | `.WillEnterForeground()` |
    | Destroying | `WillTerminate` | `.WillTerminate()` |
    
    > ⚠️ The UIKit selector names and the `AddiOS` builder method names differ for
    > activation. There is **no** `.DidBecomeActive()` or `.WillResignActive()` builder
    > method — use `.OnActivated()` and `.OnResignActivation()` or the code will not compile.
    
    ### Windows (WinUI)
    
    | Window Event | WinUI Callback |
    |---|---|
    | Created | `OnLaunched` |
    | Activated | `Activated` (foreground) |
    | Deactivated | `Activated` (background) |
    | Stopped | `VisibilityChanged` (false) |
    | Resumed | `VisibilityChanged` (true) |
    | Destroying | `Closed` |
    
    ## Hooking Native Lifecycle Directly
    
    Use `ConfigureLifecycleEvents` in `MauiProgram.cs` when you need platform-specific callbacks beyond what Window events provide:
    
    ```csharp
    builder.ConfigureLifecycleEvents(events =>
    {
    #if ANDROID
        events.AddAndroid(android => android
            .OnCreate((activity, bundle) => Debug.WriteLine("Android OnCreate"))
            .OnResume(activity => Debug.WriteLine("Android OnResume"))
            .OnPause(activity => Debug.WriteLine("Android OnPause"))
            .OnStop(activity => Debug.WriteLine("Android OnStop"))
            .OnDestroy(activity => Debug.WriteLine("Android OnDestroy")));
    #elif IOS || MACCATALYST
        events.AddiOS(ios => ios
            .OnActivated(app => Debug.WriteLine("iOS OnActivated"))
            .OnResignActivation(app => Debug.WriteLine("iOS OnResignActivation"))
            .DidEnterBackground(app => Debug.WriteLine("iOS DidEnterBackground"))
            .WillEnterForeground(app => Debug.WriteLine("iOS WillEnterForeground")));
    #elif WINDOWS
        events.AddWindows(windows => windows
            .OnLaunched((app, args) => Debug.WriteLine("Windows OnLaunched"))
            .OnActivated((window, args) => Debug.WriteLine("Windows Activated"))
            .OnClosed((window, args) => Debug.WriteLine("Windows Closed")));
    #endif
    });
    ```
    
    ## Common Pitfalls
    
    1. **Resumed does not fire on first launch.** The initial sequence is `Created` → `Activated`. Use `OnActivated` for logic that must run on every foreground entry, not `OnResumed`.
    
    2. **Deactivated ≠ Stopped.** A dialog, split-screen, or notification pull-down triggers `Deactivated` without `Stopped`. Do not perform heavy saves in `OnDeactivated` — the app may never actually background.
    
    3. **Android back button skips Stopped.** On Android, pressing back may call `Destroying` directly without `Stopped`. Place critical save logic in both `OnStopped` and `OnDestroying`.
    
    4. **Multi-window apps fire events independently.** On iPad, Mac Catalyst, and desktop Windows each `Window` instance fires its own lifecycle events. Do not assume a single global lifecycle.
    
    5. **Long-running handlers cause kills.** Android enforces a ~5 second ANR timeout; iOS has limited background execution time. Keep lifecycle handlers synchronous and fast — use `Preferences` for quick saves, not database writes.
    
    6. **Do not use legacy Xamarin.Forms lifecycle methods.** `Application.OnStart()`, `Application.OnSleep()`, and `Application.OnResume()` exist for backward compatibility but bypass Window-level events. In .NET MAUI, prefer `Window` lifecycle events (`OnActivated`, `OnStopped`, `OnResumed`, etc.) for correct multi-window behavior.
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.

Related