Repository navigation
[Android] Fix android modal back callback - #39635
ARAVIND1702 wants to merge 3 commits into
Conversation
… 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: There may be pipelines that require an authorized user to comment /azp run to run. |
|
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. |
|
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. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
[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(() => |
There was a problem hiding this comment.
[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.
This comment has been minimized.
This comment has been minimized.
kubaflo
left a comment
There was a problem hiding this comment.
Could you please check the ai's suggestions?
5f4a515 to
1ef1b30
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1ef1b30 to
4606786
Compare
kubaflo
left a comment
There was a problem hiding this comment.
Could you check the ai's suggestions? Also it looks like test failures might be related
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Tests Failure Analysis
🧪 CI Analysis — click to expand📊 maui-pr🧪 maui-pr-devicetests
🧪 maui-pr-uitestsMatching 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 refreshNext action: Inspect the Core Tests startup console; restore equivalent target diagnostics for unresolved UI variants and compare screenshots with/without the Android callback changes.
|
4606786 to
f6b9897
Compare
f6b9897 to
96b7c98
Compare
This comment has been minimized.
This comment has been minimized.
| if (!OnBackPressedDispatcher.HasEnabledCallbacks) | ||
| return false; | ||
|
|
||
| OnBackPressedDispatcher.OnBackPressed(); |
There was a problem hiding this comment.
[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() |
There was a problem hiding this comment.
[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.
PR Test Gate
|
602b6d2 to
ebb00ee
Compare
kubaflo
left a comment
There was a problem hiding this comment.
Could you please target main and check the ai's suggestions?
kubaflo
left a comment
There was a problem hiding this comment.
Could you check the ai's suggestions?
ebb00ee to
6c8fd1a
Compare
|
Closing as a stale one - please reopen or create a new one |
Description of Change
On Android, a callback registered on the Activity's
OnBackPressedDispatchernever runs while a modal page is shown.Cause: a modal is shown in its own
CustomComponentDialog(aComponentDialog), which has its own back-press dispatcher. Its callback only invoked the MAUIOnBackPressedlifecycle 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
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.NavigationPageinside a modal, nested modals, and the existing modal and back-button UI tests. I'm relying on CI for those.Issues Fixed
Fixes #39559