Skip to content

Commit 39afb8b

Browse files
[Popup] Properly handle Android Back Button pressed (#2716)
* Called `Closed` in `override OnBackButtonPressed()` * Use `Close()` * Update PopupPage.shared.cs
1 parent 0b81c1f commit 39afb8b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using CommunityToolkit.Maui.Converters;
44
using CommunityToolkit.Maui.Core;
5+
using CommunityToolkit.Maui.Extensions;
56
using Microsoft.Maui.Controls.PlatformConfiguration;
67
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
78
using Microsoft.Maui.Controls.Shapes;
@@ -43,11 +44,6 @@ public PopupPage(Popup popup, IPopupOptions popupOptions)
4344
// Only set the content if the parent constructor hasn't set the content already; don't override content if it already exists
4445
base.Content ??= new PopupPageLayout(popup, popupOptions);
4546

46-
if (Shell.Current is Shell shell)
47-
{
48-
Shell.SetPresentationMode(shell, PresentationMode.ModalNotAnimated);
49-
}
50-
5147
tapOutsideOfPopupCommand = new Command(async () =>
5248
{
5349
popupOptions.OnTappingOutsideOfPopup?.Invoke();
@@ -108,15 +104,16 @@ public async Task CloseAsync(PopupResult result, CancellationToken token = defau
108104

109105
popupClosedEventManager.HandleEvent(this, result, nameof(PopupClosed));
110106
}
111-
112-
// Prevent the Android Back Button from dismissing the Popup if CanBeDismissedByTappingOutsideOfPopup is true
107+
113108
protected override bool OnBackButtonPressed()
114109
{
110+
// Only close the Popup if PopupOptions.CanBeDismissedByTappingOutsideOfPopup is true
115111
if (popupOptions.CanBeDismissedByTappingOutsideOfPopup)
116112
{
117-
return base.OnBackButtonPressed();
113+
CloseAsync(new PopupResult(true), CancellationToken.None).SafeFireAndForget();
118114
}
119-
115+
116+
// Always return true to let the Android Operating System know that we are manually handling the Navigation request from the Android Back Button
120117
return true;
121118
}
122119

0 commit comments

Comments
 (0)