Skip to content

Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes#33213

Merged
PureWeen merged 7 commits intodotnet:inflight/currentfrom
SuthiYuvaraj:fix-32767
Jan 22, 2026
Merged

Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes#33213
PureWeen merged 7 commits intodotnet:inflight/currentfrom
SuthiYuvaraj:fix-32767

Conversation

@SuthiYuvaraj
Copy link
Contributor

@SuthiYuvaraj SuthiYuvaraj commented Dec 18, 2025

Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android. When the user presses the back button while viewing a BlazorWebView with navigable history, the entire page is popped instead of navigating back within the WebView content

RootCause

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for Android 13+ predictive back gesture support intercepts back navigation. Since BlazorWebView doesn't register a handler to check WebView.CanGoBack(), the system pops the entire page instead of allowing the WebView to navigate its internal history first, breaking the back navigation behavior that worked in .NET 9.

Description of Change

PR #32461 introduced distributed back navigation handling for Android 13+ predictive back gesture support. This PR extends that implementation to include BlazorWebView

BlazorWebViewHandler.Android.cs:

Added an AndroidLifecycle.OnBackPressed handler in ConnectHandler. The handler checks WebView.CanGoBack() to determine whether the BlazorWebView maintains internal navigation history. If history exists, it calls WebView.GoBack() and returns true, ensuring the user navigates backwards within the WebView instead of closing the page. If no internal history exists, it returns false, allowing the standard MAUI navigation stack to handle the back action.

LifecycleEventService.cs:

Added RemoveEvent<TDelegate> method to properly cleanup lifecycle event handlers and prevent memory leaks when handlers are disconnected.

Issues Fixed

Fixes #32767

Tested the behaviour on the following platforms

  • Android
  • Windows
  • iOS
  • Mac

Output Screenshot

Before Issue Fix After Issue Fix
Screen.Recording.2025-12-18.at.7.21.48.PM.mov
Screen.Recording.2025-12-18.at.7.13.08.PM.mov

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Dec 18, 2025
@vishnumenon2684 vishnumenon2684 added the community ✨ Community Contribution label Dec 18, 2025
@sheiksyedm sheiksyedm added area-blazor Blazor Hybrid / Desktop, BlazorWebView platform/android labels Dec 18, 2025
@SuthiYuvaraj SuthiYuvaraj changed the title Fix for Back navigation different between .net 9 and .net 10 blazor hybrid Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes Dec 18, 2025
@sheiksyedm sheiksyedm marked this pull request as ready for review December 18, 2025 15:27
Copilot AI review requested due to automatic review settings December 18, 2025 15:27
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 BlazorWebView back navigation on Android 13+ by implementing an OnBackPressed lifecycle event handler that checks for WebView navigation history before allowing page navigation. The fix extends the predictive back gesture support introduced in PR #32461 to include BlazorWebView components, and adds a cleanup mechanism for lifecycle event handlers to prevent memory leaks.

Key Changes

  • Adds OnBackPressed handler registration in BlazorWebViewHandler.ConnectHandler that checks WebView.CanGoBack() before allowing page pop
  • Implements RemoveEvent method in LifecycleEventService to support proper handler cleanup in DisconnectHandler
  • Updates public API surface to track the new ConnectHandler override

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
src/Core/src/LifecycleEvents/LifecycleEventService.cs Adds internal RemoveEvent method to enable lifecycle event handler cleanup
src/BlazorWebView/src/Maui/Android/BlazorWebViewHandler.Android.cs Implements ConnectHandler/DisconnectHandler overrides to register/unregister OnBackPressed handler for WebView back navigation
src/BlazorWebView/src/Maui/PublicAPI/net-android/PublicAPI.Unshipped.txt Tracks new ConnectHandler override as public API surface change

Comment on lines 106 to 111
var lifecycleService = MauiContext.Services.GetService<ILifecycleEventService>();
if (lifecycleService is LifecycleEventService concreteService)
{
concreteService.RemoveEvent(nameof(AndroidLifecycle.OnBackPressed), _onBackPressedHandler);
_onBackPressedHandler = null;
}
Copy link

Copilot AI Dec 18, 2025

Choose a reason for hiding this comment

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

The same cast pattern (ILifecycleEventService to LifecycleEventService) is repeated in both ConnectHandler and DisconnectHandler. Consider extracting this to a helper method or property to reduce code duplication and make the intent clearer. For example, a private method like TryGetLifecycleEventService() that returns the concrete service or null.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@StephaneDelcroix StephaneDelcroix left a comment

Choose a reason for hiding this comment

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

✅ APPROVE

Summary: PR correctly fixes BlazorWebView back navigation on Android 13+ by registering an OnBackPressed lifecycle event handler.

What the PR Does

  1. Adds OnBackPressed handler in ConnectHandler that checks CanGoBack() before navigating
  2. Handles multiple BlazorWebView instances with visibility/focus checks (IsAttachedToWindow, HasWindowFocus)
  3. Uses WeakReference to prevent memory leaks
  4. Adds RemoveEvent to LifecycleEventService for proper cleanup
  5. Cleans up handler in DisconnectHandler to prevent leaks

Tests Added

I've added Device Tests to verify the fix:

  • BlazorWebViewRegistersOnBackPressedHandler - Verifies handler is registered when BlazorWebView connects
  • BlazorWebViewCleansUpOnBackPressedHandlerOnDisconnect - Verifies cleanup on disconnect

Location: src/BlazorWebView/tests/DeviceTests/Elements/BlazorWebViewTests.BackNavigation.cs

Alternative Approaches Considered

  • Override OnKeyDown: Won't work with Android 13+ predictive back gestures
  • Use AndroidX OnBackPressedDispatcher directly: Bypasses MAUI's existing architecture
  • PR's approach is optimal - integrates with MAUI's lifecycle event system

Minor Notes (Non-blocking)

  • RemoveEvent kept internal (acceptable, would require API review for public)
  • Thread-safety matches existing pattern in LifecycleEventService

Confidence: High - PR follows MAUI's established patterns and addresses all edge cases.

@rmarinho
Copy link
Member

/azp run maui-pr-uitests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho
Copy link
Member

/azp run maui-pr-devicetests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@erva0008
Copy link

erva0008 commented Jan 12, 2026

@StephaneDelcroix
Hello Stephane, I tried your PR and found a possible problem.
Can you have a look at my video from today at:
#32767 (comment)

@erva0008
Copy link

erva0008 commented Jan 15, 2026

@SuthiYuvaraj Did you see my comment/videos in the linked issue at
#32767 (comment)

Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

@SubhikshaSf4851
Copy link
Contributor

@PureWeen The issue where the back button immediately exited the app instead of navigating within the BlazorWebView is now fixed.

@erva0008
Copy link

I have tested the artifacts locally on my computer from @SubhikshaSf4851 's fix.
I can confirm that it now works for me.
Both "swiping" backwards and the button press navigates correctly "inside" the Blazor application until it's in the root, and then finally closing the app (like expected)⭐

@PureWeen
Copy link
Member

/rebase

SuthiYuvaraj and others added 7 commits January 22, 2026 00:16
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Tests verify that BlazorWebViewHandler on Android:
1. Registers OnBackPressed lifecycle event handler in ConnectHandler
2. Properly cleans up the handler in DisconnectHandler

Related to: dotnet#32767
@github-project-automation github-project-automation bot moved this from Changes Requested to Approved in MAUI SDK Ongoing Jan 22, 2026
@PureWeen PureWeen changed the base branch from main to inflight/current January 22, 2026 00:27
@PureWeen PureWeen merged commit b0fec72 into dotnet:inflight/current Jan 22, 2026
1 of 12 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in MAUI SDK Ongoing Jan 22, 2026
github-actions bot pushed a commit that referenced this pull request Jan 23, 2026
…dictive Back Gesture Changes (#33213)

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android.
When the user presses the back button while viewing a BlazorWebView with
navigable history, the entire page is popped instead of navigating back
within the WebView content

### RootCause 

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for
Android 13+ predictive back gesture support intercepts back navigation.
Since BlazorWebView doesn't register a handler to check
WebView.CanGoBack(), the system pops the entire page instead of allowing
the WebView to navigate its internal history first, breaking the back
navigation behavior that worked in .NET 9.

### Description of Change

PR #32461 introduced distributed back navigation handling for Android
13+ predictive back gesture support. This PR extends that implementation
to include BlazorWebView

**BlazorWebViewHandler.Android.cs:** 

Added an `AndroidLifecycle.OnBackPressed` handler in ConnectHandler. The
handler checks `WebView.CanGoBack()` to determine whether the
BlazorWebView maintains internal navigation history. If history exists,
it calls `WebView.GoBack()` and returns true, ensuring the user
navigates backwards within the WebView instead of closing the page. If
no internal history exists, it returns false, allowing the standard MAUI
navigation stack to handle the back action.

**LifecycleEventService.cs**: 

Added `RemoveEvent<TDelegate>` method to properly cleanup lifecycle
event handlers and prevent memory leaks when handlers are disconnected.

### Issues Fixed
Fixes #32767 

### Tested the behaviour on the following platforms
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac

 
### Output Screenshot
Before Issue Fix | After Issue Fix |
|----------|----------|
|<video width="300" height="150" alt="Before Fix"
src="https://github.com/user-attachments/assets/0d6550f8-115a-4893-bb0c-ee24bff38378">|<video
width="300" height="150" alt="After Fix"
src="https://github.com/user-attachments/assets/49bf38a5-f7be-4790-8fbb-b29ab96b1ed3">|

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
PureWeen pushed a commit that referenced this pull request Jan 27, 2026
…dictive Back Gesture Changes (#33213)

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android.
When the user presses the back button while viewing a BlazorWebView with
navigable history, the entire page is popped instead of navigating back
within the WebView content

### RootCause 

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for
Android 13+ predictive back gesture support intercepts back navigation.
Since BlazorWebView doesn't register a handler to check
WebView.CanGoBack(), the system pops the entire page instead of allowing
the WebView to navigate its internal history first, breaking the back
navigation behavior that worked in .NET 9.

### Description of Change

PR #32461 introduced distributed back navigation handling for Android
13+ predictive back gesture support. This PR extends that implementation
to include BlazorWebView

**BlazorWebViewHandler.Android.cs:** 

Added an `AndroidLifecycle.OnBackPressed` handler in ConnectHandler. The
handler checks `WebView.CanGoBack()` to determine whether the
BlazorWebView maintains internal navigation history. If history exists,
it calls `WebView.GoBack()` and returns true, ensuring the user
navigates backwards within the WebView instead of closing the page. If
no internal history exists, it returns false, allowing the standard MAUI
navigation stack to handle the back action.

**LifecycleEventService.cs**: 

Added `RemoveEvent<TDelegate>` method to properly cleanup lifecycle
event handlers and prevent memory leaks when handlers are disconnected.

### Issues Fixed
Fixes #32767 

### Tested the behaviour on the following platforms
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac

 
### Output Screenshot
Before Issue Fix | After Issue Fix |
|----------|----------|
|<video width="300" height="150" alt="Before Fix"
src="https://github.com/user-attachments/assets/0d6550f8-115a-4893-bb0c-ee24bff38378">|<video
width="300" height="150" alt="After Fix"
src="https://github.com/user-attachments/assets/49bf38a5-f7be-4790-8fbb-b29ab96b1ed3">|

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
PureWeen pushed a commit that referenced this pull request Jan 29, 2026
…dictive Back Gesture Changes (#33213)

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android.
When the user presses the back button while viewing a BlazorWebView with
navigable history, the entire page is popped instead of navigating back
within the WebView content

### RootCause 

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for
Android 13+ predictive back gesture support intercepts back navigation.
Since BlazorWebView doesn't register a handler to check
WebView.CanGoBack(), the system pops the entire page instead of allowing
the WebView to navigate its internal history first, breaking the back
navigation behavior that worked in .NET 9.

### Description of Change

PR #32461 introduced distributed back navigation handling for Android
13+ predictive back gesture support. This PR extends that implementation
to include BlazorWebView

**BlazorWebViewHandler.Android.cs:** 

Added an `AndroidLifecycle.OnBackPressed` handler in ConnectHandler. The
handler checks `WebView.CanGoBack()` to determine whether the
BlazorWebView maintains internal navigation history. If history exists,
it calls `WebView.GoBack()` and returns true, ensuring the user
navigates backwards within the WebView instead of closing the page. If
no internal history exists, it returns false, allowing the standard MAUI
navigation stack to handle the back action.

**LifecycleEventService.cs**: 

Added `RemoveEvent<TDelegate>` method to properly cleanup lifecycle
event handlers and prevent memory leaks when handlers are disconnected.

### Issues Fixed
Fixes #32767 

### Tested the behaviour on the following platforms
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac

 
### Output Screenshot
Before Issue Fix | After Issue Fix |
|----------|----------|
|<video width="300" height="150" alt="Before Fix"
src="https://github.com/user-attachments/assets/0d6550f8-115a-4893-bb0c-ee24bff38378">|<video
width="300" height="150" alt="After Fix"
src="https://github.com/user-attachments/assets/49bf38a5-f7be-4790-8fbb-b29ab96b1ed3">|

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
PureWeen pushed a commit that referenced this pull request Feb 2, 2026
…dictive Back Gesture Changes (#33213)

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android.
When the user presses the back button while viewing a BlazorWebView with
navigable history, the entire page is popped instead of navigating back
within the WebView content

### RootCause 

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for
Android 13+ predictive back gesture support intercepts back navigation.
Since BlazorWebView doesn't register a handler to check
WebView.CanGoBack(), the system pops the entire page instead of allowing
the WebView to navigate its internal history first, breaking the back
navigation behavior that worked in .NET 9.

### Description of Change

PR #32461 introduced distributed back navigation handling for Android
13+ predictive back gesture support. This PR extends that implementation
to include BlazorWebView

**BlazorWebViewHandler.Android.cs:** 

Added an `AndroidLifecycle.OnBackPressed` handler in ConnectHandler. The
handler checks `WebView.CanGoBack()` to determine whether the
BlazorWebView maintains internal navigation history. If history exists,
it calls `WebView.GoBack()` and returns true, ensuring the user
navigates backwards within the WebView instead of closing the page. If
no internal history exists, it returns false, allowing the standard MAUI
navigation stack to handle the back action.

**LifecycleEventService.cs**: 

Added `RemoveEvent<TDelegate>` method to properly cleanup lifecycle
event handlers and prevent memory leaks when handlers are disconnected.

### Issues Fixed
Fixes #32767 

### Tested the behaviour on the following platforms
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac

 
### Output Screenshot
Before Issue Fix | After Issue Fix |
|----------|----------|
|<video width="300" height="150" alt="Before Fix"
src="https://github.com/user-attachments/assets/0d6550f8-115a-4893-bb0c-ee24bff38378">|<video
width="300" height="150" alt="After Fix"
src="https://github.com/user-attachments/assets/49bf38a5-f7be-4790-8fbb-b29ab96b1ed3">|

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
github-actions bot pushed a commit that referenced this pull request Feb 4, 2026
…dictive Back Gesture Changes (#33213)

<!--
!!!!!!! MAIN IS THE ONLY ACTIVE BRANCH. MAKE SURE THIS PR IS TARGETING
MAIN. !!!!!!!
-->

### Issue Description

BlazorWebView back navigation stopped working in .NET 10 on Android.
When the user presses the back button while viewing a BlazorWebView with
navigable history, the entire page is popped instead of navigating back
within the WebView content

### RootCause 

The AndroidLifecycle.OnBackPressed lifecycle event system introduced for
Android 13+ predictive back gesture support intercepts back navigation.
Since BlazorWebView doesn't register a handler to check
WebView.CanGoBack(), the system pops the entire page instead of allowing
the WebView to navigate its internal history first, breaking the back
navigation behavior that worked in .NET 9.

### Description of Change

PR #32461 introduced distributed back navigation handling for Android
13+ predictive back gesture support. This PR extends that implementation
to include BlazorWebView

**BlazorWebViewHandler.Android.cs:** 

Added an `AndroidLifecycle.OnBackPressed` handler in ConnectHandler. The
handler checks `WebView.CanGoBack()` to determine whether the
BlazorWebView maintains internal navigation history. If history exists,
it calls `WebView.GoBack()` and returns true, ensuring the user
navigates backwards within the WebView instead of closing the page. If
no internal history exists, it returns false, allowing the standard MAUI
navigation stack to handle the back action.

**LifecycleEventService.cs**: 

Added `RemoveEvent<TDelegate>` method to properly cleanup lifecycle
event handlers and prevent memory leaks when handlers are disconnected.

### Issues Fixed
Fixes #32767 

### Tested the behaviour on the following platforms
- [x] Android
- [ ] Windows
- [ ] iOS
- [ ] Mac

 
### Output Screenshot
Before Issue Fix | After Issue Fix |
|----------|----------|
|<video width="300" height="150" alt="Before Fix"
src="https://github.com/user-attachments/assets/0d6550f8-115a-4893-bb0c-ee24bff38378">|<video
width="300" height="150" alt="After Fix"
src="https://github.com/user-attachments/assets/49bf38a5-f7be-4790-8fbb-b29ab96b1ed3">|

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Stephane Delcroix <stephane@delcroix.org>
Co-authored-by: Subhiksha Chandrasekaran <subhiksha.c@syncfusion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Blazor Hybrid / Desktop, BlazorWebView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/android

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Back navigation different between .net 9 and .net 10 blazor hybrid

9 participants