App Lifecycle

Capture foreground and background transitions with the Ansight .NET SDK across Android, iOS, Mac Catalyst, and .NET MAUI.

Use Runtime.SetAppLifecycleState(...) to keep Ansight aligned with your app’s foreground and background state.

For .NET MAUI apps that initialize through builder.UseAnsight<App>() or builder.UseAnsight(options), the SDK registers these lifecycle hooks automatically. Manual wiring is only needed for non-MAUI apps, custom startup flows, or apps that initialize Ansight.Core without the MAUI app-builder extension.

Capture Foreground and Background Transitions

Use:

Runtime.SetAppLifecycleState(AppLifecycleState.Foreground);
Runtime.SetAppLifecycleState(AppLifecycleState.Background);

When the lifecycle state changes, Ansight updates the current app state snapshot and also records a reserved AppEventType.Lifecycle event:

  • App moved to foreground
  • App moved to background

Duplicate assignments are ignored, so calling the same state repeatedly does not create extra lifecycle events.

Platform Guidance

Android

The SDK ships an Android helper:

using Android.App;
using Android.Runtime;
using Ansight.Platforms.Android;

[Application]
public class MainApplication : Application
{
    public MainApplication(IntPtr handle, JniHandleOwnership ownership)
        : base(handle, ownership)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
        AndroidAppLifecycleTracker.Register(this);
    }
}

AndroidAppLifecycleTracker maps activity resume to Foreground and TrimMemory.UiHidden to Background.

If you already have your own application or activity lifecycle wiring, it is also valid to call Runtime.SetAppLifecycleState(...) directly from those callbacks.

iOS

On iOS, update Ansight from your AppDelegate:

using Ansight;
using Foundation;
using UIKit;

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    public override void OnActivated(UIApplication application)
    {
        base.OnActivated(application);
        Runtime.SetAppLifecycleState(AppLifecycleState.Foreground);
    }

    public override void DidEnterBackground(UIApplication application)
    {
        base.DidEnterBackground(application);
        Runtime.SetAppLifecycleState(AppLifecycleState.Background);
    }
}

Mac Catalyst

Mac Catalyst uses the same pattern:

using Ansight;
using Foundation;
using UIKit;

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    public override void OnActivated(UIApplication application)
    {
        base.OnActivated(application);
        Runtime.SetAppLifecycleState(AppLifecycleState.Foreground);
    }

    public override void DidEnterBackground(UIApplication application)
    {
        base.DidEnterBackground(application);
        Runtime.SetAppLifecycleState(AppLifecycleState.Background);
    }
}

.NET MAUI Guidance

In a MAUI app, prefer the app-builder integration:

using Ansight.Maui;

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();

    builder
        .UseMauiApp<App>()
        .UseAnsight<App>();

    return builder.Build();
}

UseAnsight(...) maps Android application lifecycle callbacks and iOS/Mac Catalyst foreground/background callbacks into Ansight lifecycle state.

If you manually build options, still pass them through builder.UseAnsight(options) so the MAUI lifecycle hooks are registered:

var options = Options.CreateBuilder()
    .WithAnsightMaui()
    .Build();

builder.UseAnsight(options);

Only add manual platform-file calls in a MAUI app when you intentionally skip UseAnsight(...). In that case, use the same non-MAUI Android, iOS, and Mac Catalyst patterns shown above.