Lifecycle of .NET MAUI Apps

Developing cross-platform applications comes with the challenge of managing an app’s lifecycle across various operating systems. This is especially true for .NET Multi-platform App UI (.NET MAUI) apps, which are designed to run seamlessly across different platforms. In this article, we’ll delve into the different execution states of .NET MAUI apps and how they raise cross-platform lifecycle events to ensure smooth transitions and optimal user experiences.

The Four Execution States

.NET MAUI apps operate within four distinct execution states, each representing a specific point in the app’s lifecycle. These states are:

  1. Not Running: This is the initial state when the app is not loaded into memory or hasn’t been started yet.
  2. Running: When the app is active and in the foreground, it’s considered to be running. This is when users are actively interacting with the app.
  3. Deactivated: If the app loses focus or another window gains prominence, it enters the deactivated state. The app remains in memory but is not the focused window.
  4. Stopped: When the app is no longer visible or the user switches to a different app, it enters the stopped state. There’s no guarantee of resuming from this state, as the operating system might terminate the app due to resource constraints.

Cross-Platform Lifecycle Events

.NET MAUI introduces cross-platform lifecycle events to handle these transitions between execution states effectively. These events are raised on the Window class, providing notifications to the app about state changes. Let’s take a closer look at these events:

  1. Created: Raised after the native window has been created. At this point, the cross-platform window exists, but it might not be visible yet.
  2. Activated: Triggered when the window becomes the focused window, indicating that the app is active and ready for user interaction.
  3. Deactivated: Raised when the window loses focus, even if it’s still visible. This event marks the app as no longer being the focused window.
  4. Stopped: Indicating that the window is no longer visible, this event may lead to app termination by the operating system due to resource limitations.
  5. Resumed: Occurs when an app transitions from the stopped state back to running. This event is raised only if the app was previously stopped.
  6. Destroying: Raised when the native window is being destroyed and deallocated. It’s possible for the app to use the same cross-platform window against a new native window when reopened.

Mapping Events to Platforms

These cross-platform events are mapped to specific platform events on different operating systems. Here’s how the mapping works:

  • Created: Corresponds to OnPostCreate on Android, FinishedLaunching on iOS, and Created on Windows.
  • Activated: Maps to OnResume on Android, OnActivated on iOS, and various activation events on Windows.
  • Deactivated: Corresponds to OnPause on Android, OnResignActivation on iOS, and deactivation events on Windows.
  • Stopped: Maps to OnStop on Android, DidEnterBackground on iOS, and visibility change events on Windows.
  • Resumed: Corresponds to OnRestart on Android, WillEnterForeground on iOS, and resumed events on Windows.
  • Destroying: Maps to OnDestroy on Android, WillTerminate on iOS, and closed events on Windows.

Beyond Events: Lifecycle Methods

In addition to events, the Window class provides overridable lifecycle methods that offer more control over app behavior during state transitions. These methods include:

  • OnCreated
  • OnActivated
  • OnDeactivated
  • OnStopped
  • OnResumed
  • OnDestroying
  • OnBackgrounding (iOS and Mac Catalyst specific)

Developers can leverage these methods to implement custom logic and handle specific actions during various stages of the app’s lifecycle.

Subscribing to Lifecycle Events

To make the most of these lifecycle events, developers can subscribe to them by overriding the CreateWindow method in the App class. Here’s an example of how this can be done:

namespace MyMauiApp
{
    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) =>
            {
            };

            return window;
        }
    }
}

Alternatively, developers can create a class that derives from the Window class and override its lifecycle methods as needed.

Conclusion

Understanding the lifecycle of .NET MAUI apps and how they transition between different execution states is crucial for delivering a seamless user experience across multiple platforms. By leveraging cross-platform lifecycle events and the associated mapping to native events, developers can ensure that their apps respond appropriately to various user interactions and system constraints. Additionally, the availability of overridable lifecycle methods provides developers with the flexibility to implement custom logic and handle specific tasks during different stages of the app’s lifecycle. Through effective management of lifecycle events and methods, developers can create robust and user-friendly .NET MAUI applications that excel on all supported platforms.


Posted

in

by

Tags:

Comments

Leave a comment