A lightweight, cross-platform application lifecycle manager for .NET MAUI providing foreground/background lifecycle events across Android, iOS, and Windows. Abstracts platform differences and exposes a simple event-driven API.
License
—
Deps
4
Install Size
—
Vulns
✓ 0
Published
Oct 3, 2025
$ dotnet add package Shaunebu.MAUI.LifecycleManagerA 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.
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.
Install via NuGet Package:
Install-Package Shaunebu.MAUI.LifecycleManager
Add the namespace in your MAUI project:
using Shaunebu.MAUI.LifecycleManager;
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());
}
}
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. |
| 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 | 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). |
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();
| 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 |
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.
lifecycle.OnEnterForeground += () =>
{
MySyncService.Instance.Resume();
};
lifecycle.OnEnterBackground += () =>
{
MySyncService.Instance.Pause();
};
This allows service management across platforms seamlessly.
::: 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.
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.