Skip to content

Conversation

@kubaflo
Copy link
Contributor

@kubaflo kubaflo commented Jan 14, 2026

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 calling Page.Navigation.PopAsync() directly without checking if the page wants to intercept the navigation via OnBackButtonPressed(). This differs from the system back button behavior, which properly invokes SendBackButtonPressed() through the Android lifecycle event system.

Fix: Added a call to Page?.SendBackButtonPressed() in the OnNavigateBack() method before calling PopAsync(). If the method returns true (meaning the page handled the event and wants to prevent navigation), the method returns early and skips the PopAsync() call.

Key insight: The navigation bar back button and system back button take completely different code paths in Shell:

  • System back button: Activity.OnBackPressedAndroidLifecycle.OnBackPressedShell handler → page.SendBackButtonPressed()OnBackButtonPressed()
  • Navigation bar back button: ShellToolbarTracker.OnClickOnNavigateBack()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:

  • ✅ System back button (hardware/gesture) continues to work correctly
  • ✅ Navigation bar back button now calls OnBackButtonPressed()
  • ✅ Page can prevent navigation by returning true
  • ✅ Page can allow navigation by returning false (default behavior)
  • ✅ Matches Windows platform behavior

Testing

Added UI tests that verify:

  1. OnBackButtonPressed is called when tapping the Shell navigation bar back button
  2. Returning true from OnBackButtonPressed prevents navigation
  3. The behavior matches cross-platform expectations

Test files:

  • src/Controls/tests/TestCases.HostApp/Issues/Issue33523.cs - Test page with Shell navigation
  • src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33523.cs - NUnit test verifying OnBackButtonPressed is called

Test verification:

  • ❌ Tests FAIL without the fix (bug reproduced)
  • ✅ Tests PASS with the fix (bug resolved)

… 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.
Copilot AI review requested due to automatic review settings January 14, 2026 11:50
@kubaflo kubaflo self-assigned this Jan 14, 2026
@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Jan 14, 2026
@dotnet-policy-service
Copy link
Contributor

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.

@kubaflo kubaflo added area-navigation NavigationPage area-controls-shell Shell Navigation, Routes, Tabs, Flyout and removed community ✨ Community Contribution labels Jan 14, 2026
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 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 in ShellToolbarTracker.OnNavigateBack() to allow pages to intercept navigation bar back button clicks
  • Created UI test to verify OnBackButtonPressed is called when tapping the Shell navigation bar back button
  • Test verifies the page can prevent navigation by returning true from OnBackButtonPressed()

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)]
Copy link

Copilot AI Jan 14, 2026

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.

Suggested change
[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)]

Copilot uses AI. Check for mistakes.
{
MainThread.BeginInvokeOnMainThread(async () =>
{
await DisplayAlertAsync(string.Empty, "OnBackButtonPressed", "cancel");
Copy link

Copilot AI Jan 14, 2026

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.

Suggested change
await DisplayAlertAsync(string.Empty, "OnBackButtonPressed", "cancel");
await DisplayAlert(string.Empty, "OnBackButtonPressed", "cancel");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-shell Shell Navigation, Routes, Tabs, Flyout area-navigation NavigationPage s/agent-reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OnBackButtonPressed not firing for Shell Navigation Bar button in .NET 10 SR2

1 participant