Advertisement
Beginner .NET MAUI, App Lifecycle & Windows

What are the main page lifecycle methods in MAUI?

Short answer

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).

Save .NET MAUI mock interview
Advertisement
AModel answer
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.
Likely follow-ups
  • Do these fire more than once? | Where should you unsubscribe events?
Related topics