diff --git a/src/Prism.Maui/PrismAppBuilder.cs b/src/Prism.Maui/PrismAppBuilder.cs index 61f3f19..919a369 100644 --- a/src/Prism.Maui/PrismAppBuilder.cs +++ b/src/Prism.Maui/PrismAppBuilder.cs @@ -11,6 +11,7 @@ using Prism.Regions.Adapters; using Prism.Regions.Behaviors; using Prism.Services; +using Microsoft.Maui.LifecycleEvents; using TabbedPage = Microsoft.Maui.Controls.TabbedPage; namespace Prism; @@ -45,6 +46,28 @@ internal PrismAppBuilder(IContainerExtension containerExtension, MauiAppBuilder MauiBuilder = builder; MauiBuilder.ConfigureContainer(new PrismServiceProviderFactory(RegistrationCallback)); + MauiBuilder.ConfigureLifecycleEvents(lifecycle => + { +#if ANDROID + lifecycle.AddAndroid(android => + { + android.OnBackPressed(activity => + { + var app = Application.Current; + if (app is null || !app.Windows.OfType().Any()) + return true; + + var window = app.Windows.OfType().First(); + var currentPage = MvvmHelpers.GetCurrentPage(window.Page); + var container = currentPage.GetContainerProvider(); + var navigation = container.Resolve(); + navigation.GoBackAsync(); + + return false; + }); + }); +#endif + }); ContainerLocator.ResetContainer(); ContainerLocator.SetContainerExtension(() => containerExtension);