Managing app life cycle events is one of the most common requirements when developing an application. Likewise, it’s necessary to handle the app life cycle in a cross-platform application like a .NET Multi-platform App UI (MAUI) app to make it more efficient.
In this blog, I will share how to configure app life cycle events in your .NET MAUI app with code examples.
Generally, an app has different life cycles or states. A .NET MAUI app has the following four life cycles (execution states):
Different events will be triggered when the window moves to each state.
Following are some of the predefined life cycle events available in the cross-platform apps:
Following is the mapping diagram that explains how the .NET MAUI framework will map the native events.
With .NET MAUI Preview 13, you can easily configure a life cycle event in the MauiProgram class using the MauiAppBuilder and the ConfigureLifecycleEvents extension method. This method is available in Microsoft.Maui.LifecycleEvents namespace.
Refer to the following code example for the common configuration.
using Microsoft.Maui.LifecycleEvents; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.ConfigureLifecycleEvents(AppLifecycle => { // // code to raise an event. // }); return builder.Build(); } }
You can also raise platform-specific events for a custom setup.
Currently, the following 21 Android platform-specific events are available:
You can use the compiler directives to invoke the Android platform-specific events using the AddAndroid() extension method.
Refer to the following code example. Here, we are going to invoke the OnBackPressed event to move to the previous destination.
Public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>() builder.ConfigureLifecycleEvents(AppLifecycle => { #if ANDROID AppLifecycle.AddAndroid(android => android .OnBackPressed((activity) => BackPressed())); #endif }); return builder.Build(); } static bool BackPressed() { return true; }
Currently, the following 10 iOS platform-specific events are available:
You can use the compiler directives to invoke the iOS platform-specific events using the AddiOS() extension method.
Refer to the following code example. Here, we are going to invoke the WillEnterForeground event that will raise when the app is in focus mode.
Public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>() builder.ConfigureLifecycleEvents(AppLifecycle => { #if IOS AppLifecycle.AddiOS(ios => ios .WillEnterForeground((app) => EnteredForeground()) ); #endif }); return builder.Build(); } static void EnterForeground() { }
Currently, the following 8 Windows platform-specific events are available:
You can use the compiler directives to invoke the Windows platform-specific events using the AddWindows() extension method.
Refer to the following code example. Here, we will invoke the OnNativeMessage event to access the app instance and remove the title bar.
Public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.UseMauiApp<App>() builder.ConfigureLifecycleEvents(AppLifecycle => { #if WINDOWS AppLifecycle .AddWindows(windows => windows.OnNativeMessage((app, args) => { app.ExtendsContentIntoTitleBar = false; })); #endif }); return builder.Build(); }
For more details, refer to the .NET MAUI App Life Cycle Events documentation.
Thanks for reading! In this blog, we have seen the .NET MAUI app life cycle events and how to configure them for better efficiency. Try out them and let us know your feedback in the comments below.
If you have questions, you can contact us through our support forum, support tickets, or feedback portal. We are always happy to assist you!