.NET MAUI
App Lifecycle & Windows
6 question(s)
What are the main page lifecycle methods in MAUI?
Beginner
OnAppearing runs when a page becomes visible (good for loading/refreshing data and subscribing to events) and OnDisappearing when it's navigated away (good for saving state and unsubscribing). They can fire multiple times as the user navigates back and forth, so keep them idempotent.
protected override void OnAppearing()
{
base.OnAppearing();
_vm.Load();
}
protected override void OnDisappearing()
{
base.OnDisappearing();
_vm.Unsubscribe();
}
Real-world example
A dashboard refreshes its data each time the user returns to it via OnAppearing.
Common follow-ups: Do these fire more than once? | Where should you unsubscribe events?
App Lifecycle
Shell Navigation
App Lifecycle & Windows
How do the App and Window lifecycle events relate?
Intermediate
The Application creates Windows; each Window raises Created, Activated, Deactivated, Stopped, Resumed, and Destroying. App-level state changes map to the active window's events. Use them for app-wide concerns (persist on Stopped, refresh on Resumed), while page OnAppearing/OnDisappearing handle per-screen concerns.
window.Activated += (s, e) => _analytics.TrackForeground();
window.Deactivated += (s, e) => _analytics.TrackBackground();
Real-world example
Analytics logs foreground/background transitions from window Activated/Deactivated events across the whole app.
Common follow-ups: Window events vs page events? | Which fires when backgrounded?
App Lifecycle
Windows
App Lifecycle & Windows
How do you open and manage multiple windows in MAUI?
Intermediate
On desktop (and iPad multi-window), call Application.Current.OpenWindow(new Window(new SomePage())) to open additional windows, and CloseWindow to close them. Application.Windows lists open windows. Mobile phones are single-window, so guard multi-window features by idiom.
var second = new Window(new ReportPage()) { Title = "Report" };
Application.Current!.OpenWindow(second);
Real-world example
A desktop analytics app pops out a report into its own resizable window while the main dashboard stays open.
Common follow-ups: Which platforms support multi-window? | How do you list open windows?
Windows
Multi-window
App Lifecycle & Windows
How do you persist and restore app state across launches?
Intermediate
Save lightweight state to Preferences/SecureStorage (or a file/db) in OnDisappearing or the window Stopped event, and restore it in OnAppearing/Resumed or at startup. For navigation state and drafts, serialize what's needed so a cold start can resume the user's context.
// Save on stop
Preferences.Default.Set("draft", editor.Text);
// Restore on start
editor.Text = Preferences.Default.Get("draft", string.Empty);
Real-world example
A compose screen restores an unfinished message after the OS kills the app in the background.
Common follow-ups: Where do you save state? | What survives a cold start?
App Lifecycle
Local Storage & Data
App Lifecycle & Windows
How do you handle platform lifecycle events not exposed by MAUI?
Advanced
Use ConfigureLifecycleEvents in MauiProgram to hook native events (e.g., Android OnCreate/OnResume, iOS WillEnterForeground) with strongly-typed delegates. This lets you integrate SDKs or run platform code at precise lifecycle points without editing the platform entry classes directly.
builder.ConfigureLifecycleEvents(events =>
{
#if ANDROID
events.AddAndroid(a => a.OnResume(activity =>
_sdk.OnResume()));
#endif
});
Real-world example
An analytics SDK that needs Activity.OnResume is wired through ConfigureLifecycleEvents instead of subclassing MainActivity.
Common follow-ups: Why use ConfigureLifecycleEvents? | Can you hook iOS foreground events?
App Lifecycle
Platform Integration
App Lifecycle & Windows
How do you intercept the hardware/system back button?
Advanced
Override OnBackButtonPressed on a Page (Android hardware back) and return true to indicate you've handled it (prevent default). For Shell navigation interception, use the Navigating event. iOS has no hardware back, so also handle the navigation-bar back through Shell or a custom toolbar item.
protected override bool OnBackButtonPressed()
{
if (HasUnsavedChanges) { PromptDiscard(); return true; }
return base.OnBackButtonPressed();
}
Real-world example
An editor asks to confirm discard when the user presses Android's back button with unsaved edits.
Common follow-ups: What does returning true do? | Does iOS have a hardware back?
Navigation
App Lifecycle
App Lifecycle & Windows