Skip to content

Commit 16d2864

Browse files
kubafloPureWeen
authored andcommitted
[Android] Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow - fix (#28812)
* Update ModalNavigationManager.Android.cs [Android] ModalNavigationManager * Update ModalNavigationManager.Android.cs * Remove unused back propagation logic in Android modal manager Eliminated the preventBackPropagation variable and related event handler code from ModalNavigationManager.Android.cs, simplifying the OnBackPressed lifecycle event handling.
1 parent 4592153 commit 16d2864

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

src/Controls/src/Core/Platform/ModalNavigationManager/ModalNavigationManager.Android.cs

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -415,45 +415,14 @@ public override void HandleOnBackPressed()
415415
return;
416416
}
417417

418-
Window? window = activity.GetWindow() as Window;
419-
EventHandler? eventHandler = null;
420-
eventHandler = OnPopCanceled;
421-
if (window is not null)
422-
{
423-
window.PopCanceled += eventHandler;
424-
}
425-
426-
var preventBackPropagation = false;
427-
428418
try
429419
{
430420
IPlatformApplication.Current?.Services?.InvokeLifecycleEvents<AndroidLifecycle.OnBackPressed>(del =>
431421
{
432-
preventBackPropagation = del(activity) || preventBackPropagation;
422+
del(activity);
433423
});
434424
}
435-
finally
436-
{
437-
if (window is not null && eventHandler is not null)
438-
{
439-
window.PopCanceled -= eventHandler;
440-
}
441-
}
442-
443-
if (!preventBackPropagation)
444-
{
445-
customComponentDialog.OnBackPressedDispatcher.OnBackPressed();
446-
}
447-
448-
eventHandler = null;
449-
void OnPopCanceled(object? sender, EventArgs e)
450-
{
451-
preventBackPropagation = true;
452-
if (window is not null && eventHandler is not null)
453-
{
454-
window.PopCanceled -= eventHandler;
455-
}
456-
}
425+
finally { }
457426
}
458427
}
459428
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[Issue(IssueTracker.Github, 28811, "Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow", PlatformAffected.Android)]
2+
public class Issue28811 : ContentPage
3+
{
4+
public Issue28811()
5+
{
6+
Content = new Button()
7+
{
8+
AutomationId = "NavigateToDetailPage",
9+
HeightRequest = 50,
10+
Text = "Navigate to Detail Page",
11+
Command = new Command(() => Navigation.PushModalAsync(new Issue28811DetailPage()))
12+
};
13+
}
14+
15+
class Issue28811DetailPage : ContentPage
16+
{
17+
public Issue28811DetailPage()
18+
{
19+
Content = new Label()
20+
{
21+
AutomationId = "Issue28811DetailPage",
22+
Text = "Hello, World!",
23+
};
24+
}
25+
26+
protected override bool OnBackButtonPressed()
27+
{
28+
Navigation.PopModalAsync();
29+
return false;
30+
}
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#if ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues;
7+
8+
public class Issue28811 : _IssuesUITest
9+
{
10+
public Issue28811(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
14+
public override string Issue => "Overriding back button functionality with OnBackButtonPressed returning false in a modally pushed page causes stack overflow";
15+
16+
[Test]
17+
[Category(UITestCategories.Navigation)]
18+
public void OverridingBackButtonShouldNotCauseStackOverflow()
19+
{
20+
App.WaitForElement("NavigateToDetailPage");
21+
App.Click("NavigateToDetailPage");
22+
App.WaitForElement("Issue28811DetailPage");
23+
App.Back();
24+
App.WaitForElement("NavigateToDetailPage");
25+
}
26+
}
27+
#endif

0 commit comments

Comments
 (0)