App LifeCycle in .Net MAUI

Swetha vennapusa
3 min readFeb 20, 2023

--

.Net MAUI application lifecycle includes four states

  1. Not Running
  2. Running
  3. Deactivated
  4. Stopped

The execution state of an app depends on the app’s history. For example, when an app is installed for the first time, or a device is started, the app can be considered to be not running and it isn’t loaded in the memory yet. When the app is opened it goes to a running state. If the other apps window gains focus it goes to the deactivated state. if the user returns to the app window it goes to the running state again. if the user goes to the home screen or terminates the application so that our app window is no longer visible then the app goes to a stopped state.

Managing the app lifecycle

Cross Platform lifecycle events

.NET MAUI raises cross-platform lifecycle events on the Window class when an app transitions from one state to another state. Below are the events raised

Created: This event is raised after the native window has been created. At this point, the cross-platform window will have a native window handler, but the window might not be visible yet.

Activated: This event is raised when the window has been activated, and is, or will become, the focused window.

Deactivated: This event is raised when the window of mobile apps, has lost focus.

Stopped: This event is raised when the window is no longer visible.

Resumed: This event is raised when an app resumes after being stopped. This event can only be raised if the Stopped event was previously raised.

Destroying: This event is raised when the native window is destroyed and deallocated.

Window objects provide you overridable lifecycle methods for every event raised OnCreated, OnActivated, OnDeactivated, OnStopped, OnResumed, OnDestroying, OnBackgrounding

To handle this app life cycle events override the Create Window method in your App class

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new MainPage();
}

protected override Window CreateWindow(IActivationState activationState)
{
Window window = base.CreateWindow(activationState);

window.Created += (s, e) =>
{
// Custom logic
};

return window;
}
}

Otherwise, we can create a class that inherits from Window as below

 public class MyWindow : Window
{
public MyWindow() : base()
{
}

public MyWindow(Page page) : base(page)
{
}

protected override void OnCreated()
{
// Register services
}
protected override void OnActivated()
{
}
}

The Window-derived class can then be consumed by overriding the CreateWindow method in your App class to return a MyWindow instance.


public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new MainPage();
}

protected override Window CreateWindow(IActivationState activationState)
{
MyWindow window = (MyWindow) base.CreateWindow(activationState);
return window;
}
}

Platform lifecycle events

.Net MAUI raises platform-specific events .NET MAUI defines delegates that are invoked in response to platform lifecycle events being raised

The following code shows how you can handle native events by invoking the ConfigureLifecycleEvents method in MauiAppBuilder class, which requires using Microsoft.Maui.LifecycleEvents; directive,

 public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(android => android
.OnStart((activity) => return true;))
#endif

});

return builder.Build();
}
}

We can configure the same in IOS by using compiler directives to invoke the iOS platform-specific events using the AddiOS() extension method. Similarly in Windows by using AddWindows() extension method

.Net MAUI provides 21 lifecycle events in Andriod:

OnActivityResult, OnApplicationConfigurationChanged, OnApplicationCreate, OnApplicationCreating, OnApplicationLowMemory, OnApplicationTrimMemory, OnBackPressed, OnConfigurationChanged, OnCreate, OnDestroy, OnNewIntent, OnPause, OnPostCreate, OnPostResume, OnRequestPermissionsResult, OnRestart, OnRestoreInstanceState, OnResume, OnSaveInstanceState, OnStart, OnStop

In IOS currently, there are 10 life cycle events available:

ContinueUserActivity, DidEnterBackground, FinishedLaunching, OnActivated, OnResignActivation, OpenUrl, PerformActionForShortcutItem, WillEnterForeground, WillFinishLaunching, WillTerminate

Currently, the following 8 Windows platform-specific events are available:

OnActivated, OnClosed, OnLaunched, OnLaunching, OnNativeMessage, OnResumed, OnVisibilityChanged, OnWindowCreated

Conclusion

Thanks for reading! In this blog, .NET MAUI app life cycle events are described. Try out them and let me know your feedback in the comments below.

--

--