Skip to content

[Android] Sync the navigation stack when the platform pops without a request - #40124

Merged
kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
MykhailoDav:fix-39456-back-during-push
Nov 11, 2026
Merged

kubaflo merged 2 commits into
dotnet:candidate/11.0.1xx-sr2from
MykhailoDav:fix-39456-back-during-push

Conversation

@MykhailoDav

@MykhailoDav MykhailoDav commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description of Change

On Android, pressing the system back button while an animated NavigationPage.PushAsync is still running pops the page natively without MAUI being notified: during an in-flight fragment transaction the back press is consumed by an AndroidX callback registered later than MAUI's (OnBackPressedDispatcher invokes the most recently added enabled callback). After that:

  • the Task returned by PushAsync never completes — StackNavigationManager finishes a navigation when the outgoing fragment's view is destroyed, and a cancelled transition never destroys it,
  • NavigationStack/CurrentPage still contain the pushed page while the screen shows the previous one, and the next back press is delivered to the invisible page,
  • anything awaiting that PushAsync hangs, so the app looks frozen.

Fix

StackNavigationManager now reconciles when the platform back stack pops out from under an in-flight MAUI request. On an OnDestinationChanged that was not produced by ApplyNavigationRequest itself (guarded by a flag around the request processing), while a request is pending (IsNavigating), it compares the platform back-stack depth with the cross-platform stack; when the platform has fewer entries, it trims the stack to what is actually on screen, removes the natively popped destinations from the NavGraph (the same cleanup ApplyNavigationRequestCore does for requested pops, keeping the no-caught-Java-exceptions guarantee of #39183), and completes the pending navigation through the existing NavigationFinished path — the same reconciliation IStackNavigation.NavigationFinished already performs on the Controls side (stack sync, CurrentPage, toolbar, task completion).

The path is deliberately narrow:

  • It only runs while a navigation request is in flight. A freshly registered destination listener reports the current destination — that happens when CheckForFragmentChange rebuilds the nav host after a recreation — and on a settled stack that report must not rewrite history; graph installation in CheckForFragmentChange is additionally guarded by the same flag ApplyNavigationRequest uses. Gating on an in-flight request also means the completion can only ever resolve the request that is actually pending: NavigationPage's navigation semaphore is held for that request, so a push started synchronously from an Appearing handler queues instead of installing a new completion source.
  • Shell opts out (ShellStackNavigationManager overrides ReconcilesExternallyPoppedStacks): Shell completes navigation through ShellContentNavigationFragment.OnNavigationFinished, which does not rebuild ShellSection.Stack from the completion stack, so reconciling there would leave the section stack and the manager stack disagreeing. Shell behavior is unchanged by this PR; the issue scenario is NavigationPage.

This is the outcome the issue asks for: the interrupted push is treated as cancelled, the PushAsync task completes, and the cross-platform stack matches what is displayed.

Tests

NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition (Android device test): starts an animated push and immediately pops the platform back stack — what the stolen back press does — then checks the push task completes, the stack has a single page and CurrentPage is the root again. After that it runs a follow-up push+pop under an AppDomain.FirstChanceException handler and asserts no Java.Lang.IllegalArgumentException is raised, which fails if the natively popped destination is left in the NavGraph.

Without fix With fix
Android emulator (API 37) Fails: PushAsync never completed after the platform popped the page; with only the completion fix, the exception assertion still fails with 4× No destination with ID … is on the NavController Passes. NavigationPage category 25 passed / 1 failed / 1 skipped — the one failure (BarBackgroundColor respects Android system chrome opt-out) fails identically on unmodified main on the same emulator. Shell category: 184 passed / 0 failed

Issues Fixed

Fixes #39456

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 40124

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 40124"

@dotnet-policy-service dotnet-policy-service Bot added the community ✨ Community Contribution label Nov 10, 2026
@azure-pipelines

This comment has been minimized.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Hey there @@MykhailoDav! 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 @MykhailoDav! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@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 10, 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 — 4 findings

Reviewed commit 83bfaac.

See inline comments for details.

int platformCount = 0;
NavController.IterateBackStack(_ => platformCount++);

if (platformCount == 0 || platformCount >= NavigationStack.Count)

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 Verification - Host recreation can produce this depth mismatch without a pop. For an initialized [A, B] stack, CheckForFragmentChange installs the graph on the replacement NavController with SetGraph(NavGraph, null), then constructs Callbacks. Registering the destination listener immediately reports the start destination, outside ApplyNavigationRequest and with IsInitialNavigation false. This code therefore truncates the stack to [A], and NavigationPage.NavigationFinished removes B from InternalChildren before the replacement fragment reads CurrentPage. Exclude graph restoration/listener initialization from external-pop reconciliation so recreating a host does not discard navigation history.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed: AddOnDestinationChangedListener reports the current destination to a freshly registered listener, so the Callbacks constructed in CheckForFragmentChange after SetGraph on a replacement NavController would hit the reconciliation with a one-entry platform stack and truncate real history.

Two changes in 8a45d3f close this path:

  • Reconciliation now only runs while a navigation request is actually in flight (IsNavigating); a settled stack never reconciles, so listener registration on a recreated host is a no-op.
  • CheckForFragmentChange installs the graph and the Callbacks under the same guard flag ApplyNavigationRequest uses, covering the remaining case where a host is replaced while a request is still pending.

newStack.Add(NavigationStack[i]);

UpdateNavigationStack(newStack);
NavigationFinished(NavigationView);

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 Verification - The Shell consumer does not synchronize its managed stack from this completion argument. ShellContentStackNavigationView.NavigationFinished delegates to ShellContentNavigationFragment.OnNavigationFinished, which completes the task and updates toolbar/tabs but never updates ShellSection.Stack. If a native pop interrupts a Shell push to B, this code shortens the manager’s stack to [root] and completes the push while ShellSection still contains B, added before the request by OnPushAsync. Subsequent pushes use the shortened manager stack while Shell navigation/back handling uses the unchanged section stack. Reconcile the ShellSection too, or limit this path to consumers that support shortened completion stacks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed: ShellContentNavigationFragment.OnNavigationFinished completes the pending task and updates the toolbar, but never syncs ShellSection.Stack from the completion stack, so a shortened completion would leave the section stack and the manager stack disagreeing.

Took the second option you list: the path is now limited to consumers that rebuild their state from the completion stack. StackNavigationManager gets ReconcilesExternallyPoppedStacks (default true, used by NavigationPage whose NavigationFinished does a full SyncToNavigationStack), and ShellStackNavigationManager opts out, keeping today's Shell behavior unchanged. The issue being fixed (#39456) is a NavigationPage scenario. The full Shell device-test category passes with the change (184/184 on the same emulator).

newStack.Add(NavigationStack[i]);

UpdateNavigationStack(newStack);
NavigationFinished(NavigationView);

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] Async and Threading Safety - Reconciliation can complete a different navigation started synchronously by an Appearing handler. With a settled [A, B] stack, natively pop B and have A’s one-shot Appearing handler push C. NavigationPage.NavigationFinished sets CurrentPage to A before capturing _currentNavigationCompletionSource; that setter synchronously raises Appearing. Since no request holds the semaphore, pushing C installs C’s completion source and starts its native transaction. The outer completion then captures and resolves C’s source, so PushAsync(C) completes while C is still the manager’s active request. Preserve the completion identity across stack-sync callbacks, including when reconciliation began with no pending request.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed for the settled-stack case: with no request holding the semaphore, the CurrentPage setter inside NavigationFinished can run an Appearing handler that synchronously installs a new completion source before line "_currentNavigationCompletionSource" is captured.

That window no longer exists: reconciliation is now gated on IsNavigating, so it only ever completes the request that is actually in flight — and while that request is pending, NavigationPage's semaphore is held, so a push from an Appearing handler queues on the semaphore instead of installing a new completion source. The settled-stack entry point the scenario needs is gone (see the first thread).

return;

int platformCount = 0;
NavController.IterateBackStack(_ => platformCount++);

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 and Test Coverage - This native-pop path reintroduces the caught Java exceptions fixed by #39183 (#31368/#35276). After PopBackStack, the popped destination remains in NavGraph. IterateBackStack walks that graph and calls GetBackStackEntry for the missing destination, throwing IllegalArgumentException before catching it; debuggers and FirstChanceException subscribers still observe it. Unlike ApplyNavigationRequestCore, reconciliation never removes the stale destination. The existing PoppingPagesDoesntThrowJavaExceptions test exercises only MAUI-requested pops, and the added native-pop test checks only task completion and stack state. Preserve the earlier fix on this path and extend the native-pop test with its first-chance exception assertion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed: the popped destination stayed in the NavGraph, so every later IterateBackStack probed it and raised a caught IllegalArgumentException — the regression #39183 removed for requested pops.

Reconciliation now removes the natively popped destinations from the graph in the same pass that detects them, mirroring what ApplyNavigationRequestCore does after PopBackStack. One caught probe is still raised by the detection pass itself (that is how IterateBackStack establishes back-stack membership, the pre-existing pattern), but the graph is clean from then on. The device test now asserts it: after the reconciled push completes, a follow-up push+pop runs under a FirstChanceException handler and must observe zero IllegalArgumentExceptions. That assertion fails on the previous head and passes with the fix.

@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 and failing tests?

@MauiBot

MauiBot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

PR Test Gate

@MykhailoDav — existing-test verification for commit 83bfaacb.

Result PASSED Platform Android Commit 83bfaacb


🧪 Gate analysis — click to expand

✅ Passed. The selected verification satisfied the gate's passing conditions. Unmatched, skipped, or inconclusive groups are not passing evidence.

📱 Device tests
Test group Without fix With fix
NavigationPageTests (PushCompletesWhenThePlatformPopsDuringTheTransition) ✅ Failed as expected (883s) ✅ Passed (612s)
Execution log excerpts

Without fix NavigationPageTests (PushCompletesWhenThePlatformPopsDuringTheTransition)

         "target": {
            "kind": "emulator",
            "name": "sdk_gphone_x86_64",
            "identifier": "emulator-5554",
            "operatingSystem": "Android",
            "operatingSystemVersion": "11",
            "apiLevel": 30,
            "architecture": "x86_64",
            "supportedArchitectures": [
              "x86_64",
              "x86",
              "arm64-v8a",
              "armeabi-v7a",
              "armeabi"
            ],
            "manufacturer": "Google",
            "model": "sdk_gphone_x86_64",
            "productName": "sdk_gphone_x86_64",
            "buildFingerprint": "google/sdk_gphone_x86_64/generic_x86_64_arm64:11/RSR1.201211.001/7027799:user/release-keys",
            "cpuModel": "0",
            "cpuMaxFrequencyHertz": 2000,
            "totalMemoryBytes": 2077544448
          }
        }
      }
      <<XHARNESS_RESULT_END>>
info: Attempting to remove apk 'com.microsoft.maui.controls.devicetests'..
fail: Waiting for command timed out: execution may be compromised
fail: Error: Exit code: -2
      Std out:
XHarness exit code: 1 (TESTS_FAILED)
  Passed: 0
  Failed: 1
  Skipped: 0
  Total: 1
  Tests completed with exit code: 1

With fix NavigationPageTests (PushCompletesWhenThePlatformPopsDuringTheTransition)

            "totalMemoryBytes": 8322981888
          },
          "target": {
            "kind": "emulator",
            "name": "sdk_gphone_x86_64",
            "identifier": "emulator-5554",
            "operatingSystem": "Android",
            "operatingSystemVersion": "11",
            "apiLevel": 30,
            "architecture": "x86_64",
            "supportedArchitectures": [
              "x86_64",
              "x86",
              "arm64-v8a",
              "armeabi-v7a",
              "armeabi"
            ],
            "manufacturer": "Google",
            "model": "sdk_gphone_x86_64",
            "productName": "sdk_gphone_x86_64",
            "buildFingerprint": "google/sdk_gphone_x86_64/generic_x86_64_arm64:11/RSR1.201211.001/7027799:user/release-keys",
            "cpuModel": "0",
            "cpuMaxFrequencyHertz": 2000,
            "totalMemoryBytes": 2077544448
          }
        }
      }
      <<XHARNESS_RESULT_END>>
info: Attempting to remove apk 'com.microsoft.maui.controls.devicetests'..
info: Successfully uninstalled com.microsoft.maui.controls.devicetests
XHarness exit code: 0
  Passed: 1
  Failed: 0
  Skipped: 0
  Total: 1
  Tests completed successfully

PR head: 83bfaacb.

Without-fix baseline: 13832fce.

@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 Nov 10, 2026
@kubaflo

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

Tests Failure Analysis

@MykhailoDav — test-failure analysis for commit 83bfaac.

Scope CI failures Commit 83bfaac


🧪 CI Analysis — click to expand
📊 maui-pr

No failures found.


🧪 maui-pr-devicetests

**Insufficient (redacted) the linked Android work item is flagged incomplete, without a confirmed crash diagnostic. All five target samples have incomplete device coverage. PowerShell errors wrap these work-item failures, not compilation failures.


🧪 maui-pr-uitests

**Insufficient (redacted) current failed-result collection is truncated; additional failures remain unassessed.


🧭 Follow-up — actions and refresh

Next action: collect complete results; compare device failures against the target with identical runtime/parameters.

Maintainers: comment /review tests to refresh this report.

@kubaflo
kubaflo changed the base branch from main to candidate/11.0.1xx-sr2 November 11, 2026 20:31
@kubaflo
kubaflo merged commit b5db893 into dotnet:candidate/11.0.1xx-sr2 Nov 11, 2026
161 of 185 checks passed
kubaflo added a commit that referenced this pull request Nov 11, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
@MykhailoDav

Copy link
Copy Markdown
Contributor Author

Addressed both parts of the review:

AI suggestions — all four were valid; each thread has a detailed reply. The short version, all in 8a45d3f:

  • reconciliation is now scoped to in-flight requests only (IsNavigating), which removes both the host-recreation truncation and the completion-source identity issue;
  • Shell opts out via ReconcilesExternallyPoppedStacks — its completion path doesn't rebuild ShellSection.Stack, so it keeps today's behavior (Shell device-test category: 184/184 locally);
  • the natively popped destinations are now removed from the NavGraph in the same pass, and the device test asserts no caught Java.Lang.IllegalArgumentException afterwards (that assertion fails on the previous head).

Failing tests — on the previous run, the maui-pr build/pack jobs all died inside "Provision SDK & Build BuildTasks" before compiling anything (infra provisioning, exhausted retries). Of the device-test failures, the iOS and Windows ones can't be produced by this Android-only change, and the Android CoreCLR "Gesture Does Not Leak" failure is the scheduling-sensitive leak check, not a navigation test. Locally: NavigationPage category 25 passed / 1 failed (BarBackgroundColor respects Android system chrome opt-out, fails identically on unmodified main on the same emulator) / 1 skipped. CI is re-running on the new head.

kubaflo added a commit that referenced this pull request Nov 13, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 16, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 17, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 18, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 19, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 22, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
kubaflo added a commit that referenced this pull request Nov 23, 2026
…request (#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes #39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
@kubaflo kubaflo added this to the .NET 11 SR2 milestone Nov 24, 2026
KarthikRajaKalaimani pushed a commit to KarthikRajaKalaimani/maui that referenced this pull request Nov 26, 2026
…request (dotnet#40124)

### Description of Change

On Android, pressing the system back button while an animated
`NavigationPage.PushAsync` is still running pops the page natively
without MAUI being notified: during an in-flight fragment transaction
the back press is consumed by an AndroidX callback registered later than
MAUI's (`OnBackPressedDispatcher` invokes the most recently added
enabled callback). After that:

- the `Task` returned by `PushAsync` never completes —
`StackNavigationManager` finishes a navigation when the outgoing
fragment's view is destroyed, and a cancelled transition never destroys
it,
- `NavigationStack`/`CurrentPage` still contain the pushed page while
the screen shows the previous one, and the next back press is delivered
to the invisible page,
- anything awaiting that `PushAsync` hangs, so the app looks frozen.

**Fix**

`StackNavigationManager` now reconciles when the platform back stack
pops without a MAUI request. On every `OnDestinationChanged` that was
not produced by `ApplyNavigationRequest` itself (guarded by a flag
around the request processing), it compares the platform back-stack
depth with the cross-platform stack; when the platform has fewer
entries, it trims the stack to what is actually on screen and completes
the pending navigation through the existing `NavigationFinished` path —
the same reconciliation `IStackNavigation.NavigationFinished` already
performs on the Controls side (stack sync, `CurrentPage`, toolbar, task
completion).

This is the outcome the issue asks for: the interrupted push is treated
as cancelled, the `PushAsync` task completes, and the cross-platform
stack matches what is displayed.

**Tests**


`NavigationPageTests.PushCompletesWhenThePlatformPopsDuringTheTransition`
(Android device test): starts an animated push and immediately pops the
platform back stack — what the stolen back press does — then checks the
push task completes, the stack has a single page and `CurrentPage` is
the root again.

| | Without fix | With fix |
|---|---|---|
| Android emulator (API 37) | Fails: `PushAsync never completed after
the platform popped the page` | Passes. NavigationPage category 25
passed / 1 failed / 1 skipped — the one failure (`BarBackgroundColor
respects Android system chrome opt-out`) fails identically on unmodified
`main` on the same emulator |

### Issues Fixed

Fixes dotnet#39456

Co-authored-by: Jakub Florkowski <42434498+kubaflo@users.noreply.github.com>
@marcelomoresco

Copy link
Copy Markdown
Contributor

Thanks for the fix! Could this be backported to the 10.0.x servicing branch?

We hit the same hang on 10.0.80 in production (Android 16/17, enableOnBackInvokedCallback="true"), and it also reproduces in a clean app on 10.0.80: https://github.com/marcelomoresco/maui-back-during-push-repro (details in #40662). I checked the 10.0.90, 10.0.110 and 10.0.130 tags and none of them contains this commit, so 10.x apps currently need the "re-add the back callback" workaround.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 83bfaacb Deployed Nov 10, 2026 by MykhailoDav via conclusion #2857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-navigation NavigationPage community ✨ Community Contribution platform/android s/agent-gate-passed AI verified tests catch the bug (fail without fix, pass with fix)

Projects

None yet

4 participants