-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[PR-Agent] Fix OnBackButtonPressed not firing for Shell Navigation Bar #33531
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
… back button Fixes dotnet#33523 The Shell navigation bar back button was calling PopAsync() directly without checking if the page wants to intercept navigation via OnBackButtonPressed(). This fix adds a call to SendBackButtonPressed() before popping, matching the behavior of the system back button and the Windows platform.
|
Hey there @@kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this 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 fixes an issue where the Shell navigation bar back button (top-left arrow) does not trigger OnBackButtonPressed() on Android, while the system back button and Windows platform work correctly.
Changes:
- Added
SendBackButtonPressed()call inShellToolbarTracker.OnNavigateBack()to allow pages to intercept navigation bar back button clicks - Created UI test to verify
OnBackButtonPressedis called when tapping the Shell navigation bar back button - Test verifies the page can prevent navigation by returning
truefromOnBackButtonPressed()
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/Controls/src/Core/Compatibility/Handlers/Shell/Android/ShellToolbarTracker.cs |
Added SendBackButtonPressed() call before PopAsync() in OnNavigateBack() to allow pages to intercept navigation |
src/Controls/tests/TestCases.HostApp/Issues/Issue33523.cs |
Test page with Shell navigation that overrides OnBackButtonPressed() and prevents navigation |
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33523.cs |
NUnit test verifying OnBackButtonPressed is called when tapping the navigation bar back button |
.github/agent-pr-session/pr-33523.md |
Documentation of the PR review process and test verification |
|
|
||
| namespace Maui.Controls.Sample.Issues | ||
| { | ||
| [Issue(IssueTracker.Github, 33523, "OnBackButtonPressed not firing for Shell Navigation Bar button in .NET 10 SR2", PlatformAffected.Android)] |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue description references '.NET 10 SR2' which is speculative. Based on the repository structure and global.json, the current version should be verified. Consider updating to match the actual .NET version being targeted or removing the version-specific reference if it applies broadly.
| [Issue(IssueTracker.Github, 33523, "OnBackButtonPressed not firing for Shell Navigation Bar button in .NET 10 SR2", PlatformAffected.Android)] | |
| [Issue(IssueTracker.Github, 33523, "OnBackButtonPressed not firing for Shell Navigation Bar button", PlatformAffected.Android)] |
| { | ||
| MainThread.BeginInvokeOnMainThread(async () => | ||
| { | ||
| await DisplayAlertAsync(string.Empty, "OnBackButtonPressed", "cancel"); |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name 'DisplayAlertAsync' appears to be incorrect. The standard MAUI Page API uses 'DisplayAlert' (synchronous) not 'DisplayAlertAsync'. This code sample may not compile.
| await DisplayAlertAsync(string.Empty, "OnBackButtonPressed", "cancel"); | |
| await DisplayAlert(string.Empty, "OnBackButtonPressed", "cancel"); |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description of Change
This PR fixes an issue where the Shell navigation bar back button (the arrow in the top-left corner) does not trigger
OnBackButtonPressed()on Android, while it works correctly on Windows.Root cause: The Shell navigation bar back button click handler (
ShellToolbarTracker.OnClick) was callingPage.Navigation.PopAsync()directly without checking if the page wants to intercept the navigation viaOnBackButtonPressed(). This differs from the system back button behavior, which properly invokesSendBackButtonPressed()through the Android lifecycle event system.Fix: Added a call to
Page?.SendBackButtonPressed()in theOnNavigateBack()method before callingPopAsync(). If the method returnstrue(meaning the page handled the event and wants to prevent navigation), the method returns early and skips thePopAsync()call.Key insight: The navigation bar back button and system back button take completely different code paths in Shell:
Activity.OnBackPressed→AndroidLifecycle.OnBackPressed→Shellhandler →page.SendBackButtonPressed()→OnBackButtonPressed()ShellToolbarTracker.OnClick→OnNavigateBack()→PopAsync()(directly, bypassing the check)The fix unifies these paths by ensuring both buttons check
OnBackButtonPressed()before navigating back.What to avoid: Don't bypass
SendBackButtonPressed()when programmatically popping pages in response to user navigation actions. This method is the public API contract for allowing pages to intercept back navigation.Issues Fixed
Fixes #33523
Verified behavior:
OnBackButtonPressed()truefalse(default behavior)Testing
Added UI tests that verify:
truefrom OnBackButtonPressed prevents navigationTest files:
src/Controls/tests/TestCases.HostApp/Issues/Issue33523.cs- Test page with Shell navigationsrc/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33523.cs- NUnit test verifying OnBackButtonPressed is calledTest verification: