MAUIverse MAUIverse

Migrate the deprecated OnBackPressed function in .NET MAUI Android application

Many Android apps only exit after a second tap on Back, with a toast in between. A common way to build that in a MAUI app is overriding OnBackPressed in MainActivity, and since Android 13 (SDK 33) that method is deprecated.

Android 13 introduces a window-level OnBackInvokedCallback API to support predictive back gestures, which let users preview the result of a Back gesture before completing it. In this article, the existing logic moves into an OnBackPressedCallback subclass, where HandleOnBackPressed does the same navigation-stack check, double-press timing, and toast as before. The callback is then registered in OnCreate with OnBackPressedDispatcher.AddCallback.

If your MainActivity still overrides OnBackPressed, this is a quick migration to follow.

View Source →

← Back to Community Feed

}