Skip to content
Skillpedia
Mobile apps5.8k Β· #14583

maui-app-lifecycle

What it does

This skill explains how a .NET MAUI app moves between states and how to save and restore data during those changes. It is for developers making mobile or desktop apps that must not lose user work.

When to use it

  • βœ“When you want to save a draft when the app goes to the background.
  • βœ“When you want to refresh data when the app comes back to the foreground.
  • βœ“When you want to run setup or cleanup at the right point in the app lifecycle.

Skill link

https://github.com/dotnet/skills/blob/main/plugins/dotnet-maui/skills/maui-app-lifecycle/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/dotnet/skills/blob/main/plugins/dotnet-maui/skills/maui-app-lifecycle/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œSave the user's unsent note when my MAUI app goes to the background and restore it on resume.”

5 new skills a week. 1 minute to read.

How it works

The agent uses the cross-platform window lifecycle events, adds platform hooks where needed, and puts the save and restore logic at the right point in the app's states.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

.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);
}

FAQ

What are the app states?

The app can be not running, running in the foreground, deactivated or stopped.

What is the difference between deactivated and stopped?

Deactivated means the app is visible but not in focus, while stopped means it is in the background.

Does it cover navigation?

No. Page navigation is covered by the Shell navigation skill.

Details

Original description by the author

.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

repo: dotnet/skills

Popularity

GitHub stars
5.8k
Ranking position
#14583
License
MIT
Updated
11/2/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills