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
<PackageReference Include="Shaunebu.MAUI.LifecycleManager" Version="1.0.0" />
<PackageVersion Include="Shaunebu.MAUI.LifecycleManager" Version="1.0.0" />
<PackageReference Include="Shaunebu.MAUI.LifecycleManager" />
paket add Shaunebu.MAUI.LifecycleManager --version 1.0.0
#r "nuget: Shaunebu.MAUI.LifecycleManager, 1.0.0"
#:package Shaunebu.MAUI.LifecycleManager@1.0.0
#addin nuget:?package=Shaunebu.MAUI.LifecycleManager&version=1.0.0
#tool nuget:?package=Shaunebu.MAUI.LifecycleManager&version=1.0.0
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.
Features
Cross-platform lifecycle awareness: Handles foreground and background transitions on Android, iOS, and Windows.
Event-driven API: Subscribe to
OnEnterForegroundandOnEnterBackground.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.Dispatchif 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 :::
Appinitializes the manager.LifecycleManagerhandles 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 | Versions 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. |
-
net9.0-android35.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-ios18.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-maccatalyst18.0
- Microsoft.Maui.Controls (>= 9.0.82)
-
net9.0-windows10.0.19041
- Microsoft.Maui.Controls (>= 9.0.82)
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 |