Thursday, 14 August 2025

.NET MAUI Application Lifecycle: Comprehensive Breakdown

The MAUI application lifecycle involves several stages from launch to termination, with platform-specific behaviors on iOS and Android. Here's a detailed explanation of each phase:

1. Application Startup

Common MAUI Initialization

  1. Platform Bootstrapping:

    • Native OS launches the executable

    • Platform-specific runtime initializes (Mono for Android, .NET for iOS)

  2. MauiProgram Execution:

    csharp
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder.UseMauiApp<App>();
        // Configure services, fonts, etc.
        return builder.Build();
    }
    • Dependency injection container configured

    • Application services registered

  3. App Class Instantiation:

    • App.xaml.cs constructor executes

    • Main page set via MainPage = new MainPage();

Platform-Specific Startup

Android:

  1. MainActivity (derives from MauiAppCompatActivity) launches

  2. OnCreate() called:

    java
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // MAUI initialization happens here
    }
  3. XAML handler system initializes

iOS:

  1. UIApplicationMain entry point executes

  2. MauiUIApplicationDelegate methods called:

    csharp
    [Register("AppDelegate")]
    public class AppDelegate : MauiUIApplicationDelegate
    {
        protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
    }
  3. Root view controller initialized

2. Page Lifecycle Events

MAUI Page Events (Cross-Platform)

  1. NavigatingTo (called before page appears)

  2. NavigatedTo (after page appears)

  3. NavigatingFrom (before page disappears)

  4. NavigatedFrom (after page disappears)

csharp
protected override void OnAppearing()
{
    base.OnAppearing();
    // Equivalent to NavigatedTo
    // Start animations, load data
}

protected override void OnDisappearing()
{
    base.OnDisappearing();
    // Equivalent to NavigatedFrom
    // Pause operations, save state
}

3. Application State Transitions

Foreground/Background States

Common Events:

  • Application.Current.PageAppearing

  • Application.Current.PageDisappearing

Android Specifics:

  1. Resume (OnResume()):

    • App returns to foreground

    • MAUI triggers OnResume event

  2. Pause (OnPause()):

    • App going to background

    • MAUI triggers OnSleep event

iOS Specifics:

  1. Active (OnActivated):

    • App enters foreground

    • MAUI triggers OnResume

  2. Background (DidEnterBackground):

    • App enters background

    • MAUI triggers OnSleep

4. Detailed Lifecycle Sequence

Cold Start Scenario

  1. Platform launches process

  2. MauiProgram.CreateMauiApp() executes

  3. DI container built

  4. App constructor runs

  5. MainPage created

  6. Platform-specific window created

  7. OnStart triggered (Android only)

  8. OnAppearing for initial page

  9. UI becomes interactive

Background to Foreground

  1. OS resumes app process

  2. OnResume triggered (cross-platform)

  3. Current page's OnAppearing called

  4. UI updates resume

Foreground to Background

  1. User navigates away from app

  2. Current page's OnDisappearing called

  3. OnSleep triggered (cross-platform)

  4. App may enter suspended state

5. Platform-Specific Lifecycle Details

Android Lifecycle Mapping

Android ActivityMAUI Equivalent
OnCreate()App constructor
OnStart()OnStart
OnResume()OnResume
OnPause()OnSleep
OnStop()(Part of Sleep)
OnDestroy()OnDestroy

Key Notes:

  • OnSleep combines Android's OnPause and OnStop

  • OnResume matches Android's OnResume

iOS Lifecycle Mapping

iOS EventMAUI Equivalent
FinishedLaunchingApp startup
OnActivatedOnResume
DidEnterBackgroundOnSleep
WillTerminateOnDestroy

Key Notes:

  • iOS uses scene-based lifecycle in modern apps

  • MAUI abstracts scene management

6. Lifecycle Customization

Handling Events in App.xaml.cs

csharp
public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        
        this.Startup += OnStartup;
        this.Sleep += OnAppSleep;
        this.Resume += OnAppResume;
    }
    
    private void OnAppSleep(object sender, EventArgs e)
    {
        // Save state, pause network operations
    }
    
    private void OnAppResume(object sender, EventArgs e)
    {
        // Refresh data, restart services
    }
}

Page-Level Management

csharp
public partial class MainPage : ContentPage
{
    protected override void OnAppearing()
    {
        base.OnAppearing();
        StartDataRefresh();
    }
    
    protected override void OnDisappearing()
    {
        base.OnDisappearing();
        StopDataRefresh();
    }
    
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        // Handle data context changes
    }
}

7. Special Scenarios

State Preservation

  • Android: Bundle saved in OnSaveInstanceState

  • iOS: State restoration methods

  • MAUI provides IPersistedState interface

Memory Pressure

  • Both platforms may terminate backgrounded apps

  • MAUI provides MemoryManager to monitor pressure

Multi-Window (Android)

  • Each window maintains separate lifecycle

  • Handled via OnConfigurationChanged

8. Shutdown Sequence

Normal Termination

  1. OnDisappearing for current page

  2. OnSleep triggered

  3. OnDestroy (Android) or WillTerminate (iOS)

  4. Process terminated

Abnormal Termination

  • Crash or force-stop bypasses normal lifecycle

  • MAUI provides crash reporting hooks

Understanding this complete lifecycle helps properly manage resources, maintain state, and provide smooth user experiences across all platform states.

No comments:

Post a Comment

Complete Guide: Building a Live Cricket Streaming App for 100M Users

Comprehensive guide to building a scalable live cricket streaming platform for 100M users, covering backend infrastructure, streaming techno...