Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<AndroidLifecycle.OnBackPressed>(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 { }
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue28811.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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();

@bhavanesh2001 bhavanesh2001 Apr 7, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this test should be only for Android?

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea probably, thanks!

App.WaitForElement("NavigateToDetailPage");
}
}
#endif