Previous MAUI-Shell MAUI-ShellContent Next

Shell LifeCycle

Shell Lifecycle

The Shell lifecycle defines how a Xamarin.Forms or .NET MAUI Shell application initializes, loads pages, and handles navigation. Understanding these stages helps in managing resources, navigation, and UI updates effectively.

Lifecycle Stages

  1. App Start – Application initializes and loads App.xaml.cs.
  2. Shell Initialization – Shell object is created and routes are registered.
  3. Shell Appearing – Shell's main UI is rendered.
  4. TabBar / Flyout Setup – Tabs and Flyout items are created.
  5. ShellContent Loading – Pages inside tabs or flyout items are loaded.
  6. Navigation Events – Navigation occurs via routes or user interaction.
  7. Shell Disappearing – When app closes or Shell is disposed.

Shell Lifecycle Events

  • OnAppearing() – Called when Shell or a page becomes visible.
  • OnDisappearing() – Called when Shell or a page is hidden.
  • OnNavigating() – Triggered before navigation occurs.
  • OnNavigated() – Triggered after navigation completes.

Diagram (Hierarchy + Lifecycle)

App.xaml.cs
 └── Shell
      ├── Initialize Routes
      ├── OnAppearing()
      └── TabBar / Flyout
           ├── Tab
           │    └── ShellContent → Page
           └── Navigation Events
                ├── OnNavigating()
                └── OnNavigated()
    

In .NET MAUI Shell, the lifecycle primarily revolves around the movement between pages within your app's visual hierarchy. While it respects the standard .NET MAUI app lifecycle, it introduces specific events to handle navigation and page visibility.

1. Page Visibility Events

Shell manages when pages appear or disappear. You can hook into these by overriding methods in your ContentPage:

  • OnAppearing(): Triggered just before a page becomes visible on the screen.
  • OnDisappearing(): Triggered just before a page is removed from the screen.

2. Navigation Lifecycle Events

When using Shell's URI-based navigation (Shell.Current.GoToAsync), the following sequence typically occurs:

  1. Navigating: Fired when a navigation request is initiated but before it completes.
  2. ApplyQueryAttributes: If you are passing parameters via the URL, this method is called to process those incoming values.
  3. OnAppearing: The new page prepares to show.
  4. Navigated: Fired once the navigation has successfully finished.

3. Navigation Stack Behavior

The lifecycle events fire differently depending on how you navigate:

  • Modeless Navigation: Pushing a new page onto the stack triggers OnDisappearing for the current page and OnAppearing for the new one. Popping a page does the reverse.
  • Modal Navigation: Pushing a modal page causes all visible Shell objects to raise their Disappearing event. Popping the modal raises their Appearing events.

4. Component-Level Lifecycle (Handlers)

Shell also interacts with the lower-level Handler Lifecycle, which manages the link between cross-platform code and native controls:

  • HandlerChanging: Raised before a native control is created or removed.
  • HandlerChanged: Raised once the native control is fully available and properties are applied.

More Help

I can help you dive deeper into any of these areas:

  • Would you like a code example for passing parameters using ApplyQueryAttributes?
  • Are you trying to cancel navigation using the Navigating event?
  • Do you need to handle platform-specific lifecycle events (like Android's OnPause) within a Shell app?

Example: Shell Lifecycle in Code

public partial class AppShell : Shell
{
    public AppShell()
    {
        InitializeComponent();
        Routing.RegisterRoute("details", typeof(DetailsPage));
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        Console.WriteLine("Shell Appeared");
    }

    protected override void OnNavigating(ShellNavigatingEventArgs args)
    {
        base.OnNavigating(args);
        Console.WriteLine($"Navigating to: {args.Target.Location}");
    }

    protected override void OnNavigated(ShellNavigatedEventArgs args)
    {
        base.OnNavigated(args);
        Console.WriteLine($"Navigation completed: {args.Current.Location}");
    }
}
    

Best Practices

  • 💡 Register routes in AppShell constructor for clean navigation.
  • 💡 Use OnAppearing() for UI updates, not heavy logic.
  • 💡 Use OnNavigating() to cancel navigation if needed.
  • 💡 Dispose resources in OnDisappearing() to avoid memory leaks.
  • 💡 Keep Shell lifecycle logic lightweight for better performance.
Back to Index
Previous MAUI-Shell MAUI-ShellContent Next
*