Skip to content

[Android] Fix android modal back callback - #39635

Closed
ARAVIND1702 wants to merge 3 commits into
dotnet:mainfrom
ARAVIND1702:fix-android-modal-back-callback-39559-v2
Closed

ARAVIND1702 wants to merge 3 commits into
dotnet:mainfrom
ARAVIND1702:fix-android-modal-back-callback-39559-v2

Conversation

@ARAVIND1702

Copy link
Copy Markdown

Description of Change

On Android, a callback registered on the Activity's OnBackPressedDispatcher never runs while a modal page is shown.

Cause: a modal is shown in its own CustomComponentDialog (a ComponentDialog), which has its own back-press dispatcher. Its callback only invoked the MAUI OnBackPressed lifecycle events, so the Activity's dispatcher was skipped and app-registered callbacks never got a turn.

Fix: in ModalNavigationManager.Android.cs, the dialog's callback now forwards the back press to the Activity's dispatcher when that has enabled callbacks. App callbacks run first, as they do when no modal is shown, and MAUI's own callback on the Activity still pops the modal if nothing else handles the press. When the Activity has no enabled callbacks, the previous lifecycle-event path is used, so that case is unchanged.

Testing

  • Added UI test Issue39559 (HostApp page plus NUnit test). It registers a callback on the Activity, opens a modal, presses back, and checks the callback ran. I ran it locally on a Samsung Galaxy M31 (Android 12): it passes with the fix and fails without it.
  • Manual check in the Sandbox app on a Pixel emulator image (Android 16) and the same Galaxy M31:
    • A user callback registered on the Activity, back with a modal open: the callback now fires. Before the fix it did not.
    • No user callback, back with a modal open: the modal still closes and the app stays open.
  • Not tested: Shell, NavigationPage inside a modal, nested modals, and the existing modal and back-button UI tests. I'm relying on CI for those.

Issues Fixed

Fixes #39559

ARAVIND1702 and others added 2 commits October 25, 2026 23:15
… modal is open

The modal's CustomComponentDialog has its own OnBackPressedDispatcher whose
callback invoked only the MAUI OnBackPressed lifecycle events, so callbacks
registered by the app on the Activity's dispatcher never ran while a modal
was shown.

Route the back press through the Activity's dispatcher when it has enabled
callbacks, so app callbacks run first (as without a modal) and MAUI's own
callback still pops the modal if nothing else handles it. Falls back to the
previous lifecycle-event path otherwise.

Fixes dotnet#39559

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Verifies that an OnBackPressedCallback registered on the Activity's
dispatcher fires when back is pressed while a modal page is shown.

Related to dotnet#39559

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Oct 25, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@ARAVIND1702! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @ARAVIND1702! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@ARAVIND1702 ARAVIND1702 changed the title Fix android modal back callback 39559 v2 Fix android modal back callback Oct 25, 2026
@ARAVIND1702 ARAVIND1702 changed the title Fix android modal back callback [Android] Fix android modal back callback Oct 25, 2026
@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 25, 2026
@kubaflo

This comment has been minimized.

@azure-pipelines

This comment has been minimized.

@github-actions github-actions Bot left a comment

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.

Expert Review — 2 findings

Reviewed commit d5e9896.

Source assessment incomplete (6/7 dimensions); finding count is partial.

See inline comments for details.

if (activity is AndroidX.Activity.ComponentActivity componentActivity &&
componentActivity.OnBackPressedDispatcher.HasEnabledCallbacks)
{
componentActivity.OnBackPressedDispatcher.OnBackPressed();

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.

[major] Logic and Correctness - Preserve the dialog-scoped fallback when modal back handling returns false. With no app callback registered, a modal still enables MAUI's Activity callback through Window.CanConsumeBackNavigation. Forwarding here reaches MauiAppCompatActivity.HandleBackNavigation(), which disables its callback and calls base.OnBackPressed() if the lifecycle delegates return false. For example, cancelling Window.ModalPopping makes Page.OnBackButtonPressed return false, so back can finish/background the host Activity instead of keeping the cancelled modal onscreen. The existing Issue28811 modal, which calls PopModalAsync() and returns false, likewise propagates back beyond its modal pop. The previous dialog path deliberately ignored that return value; pinned history associates that behavior with #28812. Keep app callbacks reachable without restoring Activity fallback for these modal scenarios. The new test's consuming app callback bypasses both cases.

if (!s_callbackRegistered && Microsoft.Maui.ApplicationModel.Platform.CurrentActivity is AndroidX.Activity.ComponentActivity activity)
{
s_callbackRegistered = true;
activity.OnBackPressedDispatcher.AddCallback(activity, new Issue39559Callback(() =>

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.

[major] Regression Prevention - Scope this callback to the issue page rather than the entire Activity. AddCallback(activity, ...) keeps the always-enabled callback registered until Activity destruction, but HandleOnBackPressed never forwards to another callback and the page never removes or disables it. Open this issue, close its modal, then leave through toolbar navigation: hardware back on later pages is still consumed by Issue39559Callback instead of navigating. The static Updated delegate also retains the departed page/modal label. Store and unregister the callback and clear the static update target when leaving, with registration restored on re-entry; reset or baseline the static count as well so reopening the test does not expect 1 after a previous invocation.

@MauiBot

This comment has been minimized.

@MauiBot MauiBot added s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) and removed s/agent-review-in-progress AI review is currently running for this PR labels Oct 25, 2026

@kubaflo kubaflo left a comment

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.

Could you please check the ai's suggestions?

@kubaflo

This comment has been minimized.

@github-actions

This comment has been minimized.

@kubaflo kubaflo left a comment

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.

Could you check the ai's suggestions? Also it looks like test failures might be related

@azure-pipelines

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Oct 31, 2026
@MauiBot

This comment has been minimized.

@MauiBot MauiBot removed the s/agent-review-in-progress AI review is currently running for this PR label Oct 31, 2026
@kubaflo

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Tests Failure Analysis

@ARAVIND1702 — test-failure analysis for commit e56132e.

Scope CI failures Commit e56132e


🧪 CI Analysis — click to expand
📊 maui-pr
  • 🟢 Likely unrelated — Windows Debug, Release, Pack: missing GroupHeaderStyleSelector, FlyoutPageControl, and FormsCommandBar repeat on target; Android-only changes do not alter Compatibility compilation.

🧪 maui-pr-devicetests
  • 🟡 Needs human investigation — MacCatalyst Mono Core Tests: macOS 26 ARM64 timed out after 3,600 seconds with zero tests; this caused the Windows-hosted dispatch failure, not a compiler error. Target Helix coverage is incomplete.

🧪 maui-pr-uitests

Matching diagnostics below recur on inflight/current; the diff changes neither these snapshots nor their setup. All five history samples are truncated; unmatched cases remain unresolved.


🧭 Follow-up — actions and refresh

Next action: Inspect the Core Tests startup console; restore equivalent target diagnostics for unresolved UI variants and compare screenshots with/without the Android callback changes.

Maintainers: comment /review tests to refresh this report.

@kubaflo

This comment has been minimized.

@github-actions github-actions Bot added the s/agent-review-in-progress AI review is currently running for this PR label Nov 3, 2026

@github-actions github-actions Bot left a comment

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.

Expert Review — 2 findings

Reviewed commit e56132e.

See inline comments for details.

if (!OnBackPressedDispatcher.HasEnabledCallbacks)
return false;

OnBackPressedDispatcher.OnBackPressed();

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.

[major] Logic and Correctness - Preserve modal-scoped fallback when an enabled Activity callback yields. An app callback can disable itself and redispatch to let navigation handle back, but MAUI's callback remains disabled throughout this call. With no other enabled callback, that nested dispatch reaches the Activity's default back action instead of popping the modal; the outer call then returns true and the dialog skips its lifecycle fallback. This also affects the existing BlazorWebViewBackCallback: give the underlying WebView back history, then push a native modal. Its callback remains enabled but the WebView loses window focus, so it disables itself and redispatches, finishing/backgrounding the host instead of closing the modal. Provide a modal-scoped terminal handler for callbacks that yield, without restoring Activity fallback for cancelled modal pops. Add a delegating-callback regression case that verifies modal navigation and that the host stays open; the new fixture only tests an unconditionally consuming callback.


[Test]
[Category(UITestCategories.Navigation)]
public void ActivityBackCallbackFiresWhenModalIsOpen()

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.

[moderate] Regression Prevention - Protect the negative branch introduced by the dispatcher guard. This fixture always registers an enabled callback that consumes back, so it never verifies that an app callback registered with Enabled=false is skipped and normal modal navigation still runs, or that cancelling Window.ModalPopping with no enabled app callback keeps both the modal and host onscreen. Those cases are the reason the new helper must exclude MAUI's own callback before checking HasEnabledCallbacks; a version that forwards directly to the Activity dispatcher would still satisfy this test while restoring the previous fallback regression. Existing Issue28811 covers a modal override that pops and returns false, not these disabled-callback/cancellation cases. Add discriminating assertions for these negative paths.

@MauiBot

MauiBot commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@ARAVIND1702 — existing-test verification for commit e56132e7.

Result INCONCLUSIVE Platform Android Commit e56132e7


🧪 Gate analysis — click to expand

⚠ Inconclusive. Setup, build, or execution did not produce a conclusive result. This does not prove the fix is incorrect.

No interpretable test-group summary was produced. No additional test execution is inferred.

PR head: e56132e7.

Without-fix baseline: 96b7c982.

@MauiBot MauiBot removed s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix) s/agent-review-in-progress AI review is currently running for this PR labels Nov 3, 2026
@kubaflo
kubaflo force-pushed the inflight/current branch 2 times, most recently from 602b6d2 to ebb00ee Compare November 7, 2026 21:17

@kubaflo kubaflo left a comment

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.

Could you please target main and check the ai's suggestions?

@kubaflo kubaflo left a comment

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.

Could you check the ai's suggestions?

@kubaflo
kubaflo changed the base branch from inflight/current to main November 13, 2026 19:24
@kubaflo

kubaflo commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Closing as a stale one - please reopen or create a new one

@kubaflo kubaflo closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community ✨ Community Contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HandleOnBackPressed Fails to Trigger with Modal Open on Android

3 participants