Shaunebu.MAUI.LifecycleManager 1.0.0

dotnet add package Shaunebu.MAUI.LifecycleManager --version 1.0.0
                    
NuGet\Install-Package Shaunebu.MAUI.LifecycleManager -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Shaunebu.MAUI.LifecycleManager" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Shaunebu.MAUI.LifecycleManager" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Shaunebu.MAUI.LifecycleManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Shaunebu.MAUI.LifecycleManager --version 1.0.0
                    
#r "nuget: Shaunebu.MAUI.LifecycleManager, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Shaunebu.MAUI.LifecycleManager@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Shaunebu.MAUI.LifecycleManager&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Shaunebu.MAUI.LifecycleManager&version=1.0.0
                    
Install as a Cake Tool

Shaunebu.MAUI.LifecycleManager 📗

A lightweight, cross-platform application lifecycle manager for .NET MAUI. This library provides foreground/background lifecycle events across Android, iOS, and Windows, allowing developers to react to app state changes in a consistent way. It abstracts platform differences and exposes a simple event-driven API.

NuGet Version

NET Support NET Support NET Support Support


Features

  • Cross-platform lifecycle awareness: Handles foreground and background transitions on Android, iOS, and Windows.

  • Event-driven API: Subscribe to OnEnterForeground and OnEnterBackground.

  • Simple initialization: Minimal setup in App.xaml.cs.

  • Thread-safe callbacks: All events are marshaled appropriately to ensure safe UI interactions.

  • No external dependencies: Pure MAUI library.


Installation

Install via NuGet Package:

Install-Package Shaunebu.MAUI.LifecycleManager

Add the namespace in your MAUI project:

using Shaunebu.MAUI.LifecycleManager;


Initialization

Initialize the library in App.xaml.cs:

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

        // 1️⃣ Create lifecycle manager
        var lifecycle = LifecycleManager.Current;

        // 2️⃣ Subscribe to lifecycle events
        lifecycle.OnEnterForeground += () =>
        {
            Console.WriteLine("✅ App entered foreground");
        };

        lifecycle.OnEnterBackground += () =>
        {
            Console.WriteLine("🌙 App entered background");
        };

        // 3️⃣ Initialize (wires platform-specific events)
        lifecycle.Initialize();
    }

    protected override Window CreateWindow(IActivationState? activationState)
    {
        return new Window(new AppShell());
    }
}

API Reference

Class: LifecycleManager

Property / Event Type Description
Current LifecycleManager Singleton instance of the lifecycle manager.
OnEnterForeground Action Triggered when the app enters the foreground.
OnEnterBackground Action Triggered when the app enters the background.

Methods

Method Parameters Description
Initialize() None Subscribes platform-specific lifecycle events and starts tracking.
InvokeForeground() None Manually trigger foreground callbacks.
InvokeBackground() None Manually trigger background callbacks.

Platform Behavior

Platform Foreground Detection Background Detection Notes
Android OnResume / OnStart OnPause Works in activities lifecycle; supports multiple windows.
iOS WillEnterForeground DidEnterBackground Requires partial class on AppDelegate.
Windows Window.Activated Window.VisibilityChanged Background detection is approximate (visibility-based).

Usage Example

var lifecycle = LifecycleManager.Current;

lifecycle.OnEnterForeground += () =>
{
    // Refresh UI or reconnect services
    Console.WriteLine("App is now in foreground");
};

lifecycle.OnEnterBackground += () =>
{
    // Pause timers, save state, or disconnect services
    Console.WriteLine("App moved to background");
};

lifecycle.Initialize();

Comparison With Other Approaches

Approach Pros Cons
Manual platform hooks (AppDelegate, MainActivity) Full control Repetitive, platform-specific, verbose
MessagingCenter / EventAggregator Decoupled Boilerplate, hard to maintain cross-platform consistency
Shaunebu.MAUI.Lifecycle Unified API, cross-platform, minimal setup Limited to foreground/background events, no full lifecycle details

Notes & Best Practices

  • Always initialize the manager once, preferably in App.xaml.cs.

  • Use the events to pause or resume services, timers, or network requests.

  • On Windows, background detection is approximate; consider additional checks for multi-window apps.

  • Events are safe to call UI code, but consider Dispatcher.Dispatch if you encounter threading issues.

  • No extra configuration is required for Android/iOS/Windows, but for advanced use cases you may extend the library with custom platform hooks.


Example: Integrating With Services

lifecycle.OnEnterForeground += () =>
{
    MySyncService.Instance.Resume();
};

lifecycle.OnEnterBackground += () =>
{
    MySyncService.Instance.Pause();
};

This allows service management across platforms seamlessly.


Diagram

::: mermaid flowchart TD; App --> LifecycleManager LifecycleManager -->|Foreground| ForegroundEvent LifecycleManager -->|Background| BackgroundEvent ForegroundEvent --> Services BackgroundEvent --> Services :::

  • App initializes the manager.

  • LifecycleManager handles platform-specific hooks.

  • Events notify services or UI logic of state changes.


FAQ

Q: Do I need extra permissions?
A: No, all events are internal hooks to MAUI lifecycle.

Q: Can I manually trigger foreground/background?
A: Yes, via InvokeForeground() and InvokeBackground().

Q: Can it detect app termination?
A: No, termination is not detectable reliably across all platforms.

Product Compatible and additional computed target framework versions.
.NET net9.0-android35.0 is compatible.  net9.0-ios18.0 is compatible.  net9.0-maccatalyst18.0 is compatible.  net9.0-windows10.0.19041 is compatible.  net10.0-android was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 121 10/3/2025