diff --git a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs index 579691c45bf7..d1194596b568 100644 --- a/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs +++ b/src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs @@ -415,45 +415,14 @@ public override void HandleOnBackPressed() return; } - Window? window = activity.GetWindow() as Window; - EventHandler? eventHandler = null; - eventHandler = OnPopCanceled; - if (window is not null) - { - window.PopCanceled += eventHandler; - } - - var preventBackPropagation = false; - try { IPlatformApplication.Current?.Services?.InvokeLifecycleEvents(del => { - preventBackPropagation = del(activity) || preventBackPropagation; + del(activity); }); } - finally - { - if (window is not null && eventHandler is not null) - { - window.PopCanceled -= eventHandler; - } - } - - if (!preventBackPropagation) - { - customComponentDialog.OnBackPressedDispatcher.OnBackPressed(); - } - - eventHandler = null; - void OnPopCanceled(object? sender, EventArgs e) - { - preventBackPropagation = true; - if (window is not null && eventHandler is not null) - { - window.PopCanceled -= eventHandler; - } - } + finally { } } } } diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue28811.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue28811.cs new file mode 100644 index 000000000000..fb49af9f01eb --- /dev/null +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue28811.cs @@ -0,0 +1,32 @@ +[Issue(IssueTracker.Github, 28811, "Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow", PlatformAffected.Android)] +public class Issue28811 : ContentPage +{ + public Issue28811() + { + Content = new Button() + { + AutomationId = "NavigateToDetailPage", + HeightRequest = 50, + Text = "Navigate to Detail Page", + Command = new Command(() => Navigation.PushModalAsync(new Issue28811DetailPage())) + }; + } + + class Issue28811DetailPage : ContentPage + { + public Issue28811DetailPage() + { + Content = new Label() + { + AutomationId = "Issue28811DetailPage", + Text = "Hello, World!", + }; + } + + protected override bool OnBackButtonPressed() + { + Navigation.PopModalAsync(); + return false; + } + } +} \ No newline at end of file diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28811.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28811.cs new file mode 100644 index 000000000000..e3c6abce5eda --- /dev/null +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue28811.cs @@ -0,0 +1,27 @@ +#if ANDROID +using NUnit.Framework; +using UITest.Appium; +using UITest.Core; + +namespace Microsoft.Maui.TestCases.Tests.Issues; + +public class Issue28811 : _IssuesUITest +{ + public Issue28811(TestDevice testDevice) : base(testDevice) + { + } + + public override string Issue => "Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow"; + + [Test] + [Category(UITestCategories.Navigation)] + public void OverridingBackButtonShouldNotCauseStackOverflow() + { + App.WaitForElement("NavigateToDetailPage"); + App.Click("NavigateToDetailPage"); + App.WaitForElement("Issue28811DetailPage"); + App.Back(); + App.WaitForElement("NavigateToDetailPage"); + } +} +#endif \ No newline at end of file