Skip to content

Conversation

@TheCodeTraveler
Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler commented Jun 13, 2025

Description of Change

This PR updates protected override bool PopupPage.OnBackButtonPressed() to ensure we properly close the Popup using CloseAsync() when the Android Back Button is tapped by the user.

This ensures that the Task returned by ShowPopupAsync() completes when the Android Back Button is tapped.

Linked Issues

Fixes #2717
Fixes #2700

This fixes the Issue highlighted by @crwsolutions in their comment here: #2703 (comment).

Note: This does not close resolve the issue #2703. We currently unable to reproduce that Issue and are awaiting a sample reproduction to help us solve that Issue.

PR Checklist

Additional information

SafeFireAndForget
Because protected override bool OnBackButtonPressed() does not return a Task, we are unable to use the await keyword. Since PopupPage.Close returns a Task, we use .SafeFireAndForget() to ensure we follow best practices and await its Task under the hood.

PopupResult
For the PopupResult, I am setting WasDismissedByTappingOutsideOfPopup to true for two reasons:

  1. By tapping the Android Back button, the user did tap outside of the popup to close it
  2. WasDismissedByTappingOutsideOfPopup = false requires PopupResult<T>.Result to be a value provided by the developer when programmatically closing the popup

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that tapping the Android Back button properly closes the popup by calling CloseAsync() and uses SafeFireAndForget(), so the ShowPopupAsync() task completes as expected.

  • Removed the unused Shell.SetPresentationMode block in the constructor.
  • Added CommunityToolkit.Maui.Extensions for SafeFireAndForget.
  • Updated OnBackButtonPressed to always call CloseAsync(...) and return true instead of delegating to base.
Comments suppressed due to low confidence (1)

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

  • Consider adding unit or integration tests for OnBackButtonPressed to verify that CloseAsync is invoked and that ShowPopupAsync() completes when the Android Back button is pressed.
CloseAsync(new PopupResult(true), CancellationToken.None).SafeFireAndForget();

bijington
bijington previously approved these changes Jun 13, 2025
@TheCodeTraveler TheCodeTraveler enabled auto-merge (squash) June 13, 2025 18:48
@TheCodeTraveler TheCodeTraveler added the approved This Proposal has been approved and is ready to be added to the Toolkit label Jun 13, 2025
@TheCodeTraveler TheCodeTraveler merged commit 39afb8b into main Jun 13, 2025
10 checks passed
@TheCodeTraveler TheCodeTraveler deleted the Close-Popup-When-Android-Back-Button-Pressed branch June 13, 2025 21:51
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

approved This Proposal has been approved and is ready to be added to the Toolkit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Popup V2 - Android - ShowPopupAsync Task never completes on System Back (Button) [BUG] Popup v2 - App Crash with secondary Window

2 participants