Advertisement
Advanced .NET MAUI, App Lifecycle & Windows

How do you handle platform lifecycle events not exposed by MAUI?

Short answer

Use ConfigureLifecycleEvents in MauiProgram to hook native events (e.g., Android OnCreate/OnResume, iOS WillEnterForeground) with strongly-typed delegates.

Save .NET MAUI mock interview
Advertisement
AModel answer
Use ConfigureLifecycleEvents in MauiProgram to hook native events (e.g., Android OnCreate/OnResume, iOS WillEnterForeground) with strongly-typed delegates. This lets you integrate SDKs or run platform code at precise lifecycle points without editing the platform entry classes directly.
builder.ConfigureLifecycleEvents(events =>
{
#if ANDROID
    events.AddAndroid(a => a.OnResume(activity =>
        _sdk.OnResume()));
#endif
});
Real-world example
An analytics SDK that needs Activity.OnResume is wired through ConfigureLifecycleEvents instead of subclassing MainActivity.
Likely follow-ups
  • Why use ConfigureLifecycleEvents? | Can you hook iOS foreground events?
Related topics