-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes #33213
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?
Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes #33213
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| using Microsoft.Maui; | ||
| using Microsoft.Maui.Dispatching; | ||
| using Microsoft.Maui.Handlers; | ||
| using Microsoft.Maui.LifecycleEvents; | ||
| using static global::Android.Views.ViewGroup; | ||
| using AWebView = global::Android.Webkit.WebView; | ||
| using Path = System.IO.Path; | ||
|
|
@@ -21,6 +22,7 @@ public partial class BlazorWebViewHandler : ViewHandler<IBlazorWebView, AWebView | |
| private WebChromeClient? _webChromeClient; | ||
| private AndroidWebKitWebViewManager? _webviewManager; | ||
| internal AndroidWebKitWebViewManager? WebviewManager => _webviewManager; | ||
| private AndroidLifecycle.OnBackPressed? _onBackPressedHandler; | ||
|
|
||
| private ILogger? _logger; | ||
| internal ILogger Logger => _logger ??= Services!.GetService<ILogger<BlazorWebViewHandler>>() ?? NullLogger<BlazorWebViewHandler>.Instance; | ||
|
|
@@ -60,10 +62,55 @@ protected override AWebView CreatePlatformView() | |
| return blazorAndroidWebView; | ||
| } | ||
|
|
||
| protected override void ConnectHandler(AWebView platformView) | ||
| { | ||
| base.ConnectHandler(platformView); | ||
|
|
||
| // Register OnBackPressed lifecycle event handler to check WebView's back navigation | ||
| // This ensures predictive back gesture (Android 13+) checks WebView.CanGoBack() before popping page | ||
| var services = MauiContext?.Services; | ||
| if (services != null) | ||
| { | ||
| // Create a weak reference to avoid memory leaks | ||
| var weakPlatformView = new WeakReference<AWebView>(platformView); | ||
|
|
||
| AndroidLifecycle.OnBackPressed handler = (activity) => | ||
| { | ||
| // Check if WebView is still alive and can navigate back | ||
| if (weakPlatformView.TryGetTarget(out var webView) && webView.CanGoBack()) | ||
| { | ||
| webView.GoBack(); | ||
| return true; // Prevent back propagation - handled by WebView | ||
| } | ||
|
|
||
| return false; // Allow back propagation - let page be popped | ||
| }; | ||
SuthiYuvaraj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Register with lifecycle service - will be invoked by HandleBackNavigation in MauiAppCompatActivity | ||
| var lifecycleService = services.GetService<ILifecycleEventService>(); | ||
| if (lifecycleService is LifecycleEventService concreteService) | ||
| { | ||
| concreteService.AddEvent(nameof(AndroidLifecycle.OnBackPressed), handler); | ||
| _onBackPressedHandler = handler; | ||
| } | ||
|
||
| } | ||
| } | ||
|
|
||
| private const string AndroidFireAndForgetAsyncSwitch = "BlazorWebView.AndroidFireAndForgetAsync"; | ||
|
|
||
| protected override void DisconnectHandler(AWebView platformView) | ||
| { | ||
| // Clean up lifecycle event handler to prevent memory leaks | ||
| if (_onBackPressedHandler != null && MauiContext?.Services != null) | ||
| { | ||
| var lifecycleService = MauiContext.Services.GetService<ILifecycleEventService>(); | ||
| if (lifecycleService is LifecycleEventService concreteService) | ||
| { | ||
| concreteService.RemoveEvent(nameof(AndroidLifecycle.OnBackPressed), _onBackPressedHandler); | ||
| _onBackPressedHandler = null; | ||
| } | ||
|
||
| } | ||
|
||
|
|
||
| platformView.StopLoading(); | ||
|
|
||
| if (_webviewManager != null) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| #nullable enable | ||
| override Microsoft.AspNetCore.Components.WebView.Maui.BlazorWebViewHandler.ConnectHandler(Android.Webkit.WebView! platformView) -> void |
Uh oh!
There was an error while loading. Please reload this page.