-
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
Merged
Merged
Fix for BlazorWebView Back Navigation Issues on Android 13+ After Predictive Back Gesture Changes #33213
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
356a931
fix-lifecycleEvent
SuthiYuvaraj 1401aa7
Added RemoveEvent
SuthiYuvaraj 2e08b43
remove publicAPI
SuthiYuvaraj 90917be
Apply suggestion from @Copilot
SuthiYuvaraj 14d7d07
Update BlazorWebViewHandler.Android.cs
SuthiYuvaraj 778ac31
Add Device Tests for BlazorWebView OnBackPressed handler registration
StephaneDelcroix 4714458
Fix for back button navigation
SubhikshaSf4851 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/BlazorWebView/src/Maui/PublicAPI/net-android/PublicAPI.Unshipped.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
111 changes: 111 additions & 0 deletions
111
src/BlazorWebView/tests/DeviceTests/Elements/BlazorWebViewTests.BackNavigation.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| using System.Collections.Generic; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Components.WebView.Maui; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Maui.LifecycleEvents; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.MauiBlazorWebView.DeviceTests.Elements; | ||
|
|
||
| public partial class BlazorWebViewTests | ||
| { | ||
| #if ANDROID | ||
| /// <summary> | ||
| /// Verifies that BlazorWebViewHandler registers an OnBackPressed lifecycle event handler | ||
| /// when connected on Android. This handler is essential for proper back navigation within | ||
| /// the BlazorWebView on Android 13+ with predictive back gestures. | ||
| /// See: https://github.com/dotnet/maui/issues/32767 | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task BlazorWebViewRegistersOnBackPressedHandler() | ||
| { | ||
| EnsureHandlerCreated(additionalCreationActions: appBuilder => | ||
| { | ||
| appBuilder.Services.AddMauiBlazorWebView(); | ||
| }); | ||
|
|
||
| var bwv = new BlazorWebViewWithCustomFiles | ||
| { | ||
| HostPage = "wwwroot/index.html", | ||
| CustomFiles = new Dictionary<string, string> | ||
| { | ||
| { "index.html", TestStaticFilesContents.DefaultMauiIndexHtmlContent }, | ||
| }, | ||
| }; | ||
| bwv.RootComponents.Add(new RootComponent { ComponentType = typeof(MauiBlazorWebView.DeviceTests.Components.NoOpComponent), Selector = "#app", }); | ||
|
|
||
| await InvokeOnMainThreadAsync(async () => | ||
| { | ||
| var bwvHandler = CreateHandler<BlazorWebViewHandler>(bwv); | ||
| var platformWebView = bwvHandler.PlatformView; | ||
| await WebViewHelpers.WaitForWebViewReady(platformWebView); | ||
|
|
||
| // Get the lifecycle event service and verify OnBackPressed handler is registered | ||
| var lifecycleService = MauiContext.Services.GetService<ILifecycleEventService>() as LifecycleEventService; | ||
| Assert.NotNull(lifecycleService); | ||
|
|
||
| // Verify the OnBackPressed event has been registered | ||
| Assert.True(lifecycleService.ContainsEvent(nameof(AndroidLifecycle.OnBackPressed)), | ||
| "BlazorWebViewHandler should register an OnBackPressed lifecycle event handler on Android"); | ||
| }); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that BlazorWebViewHandler properly cleans up the OnBackPressed lifecycle event handler | ||
| /// when disconnected. This prevents memory leaks and ensures proper cleanup. | ||
| /// See: https://github.com/dotnet/maui/issues/32767 | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task BlazorWebViewCleansUpOnBackPressedHandlerOnDisconnect() | ||
| { | ||
| EnsureHandlerCreated(additionalCreationActions: appBuilder => | ||
| { | ||
| appBuilder.Services.AddMauiBlazorWebView(); | ||
| }); | ||
|
|
||
| var bwv = new BlazorWebViewWithCustomFiles | ||
| { | ||
| HostPage = "wwwroot/index.html", | ||
| CustomFiles = new Dictionary<string, string> | ||
| { | ||
| { "index.html", TestStaticFilesContents.DefaultMauiIndexHtmlContent }, | ||
| }, | ||
| }; | ||
| bwv.RootComponents.Add(new RootComponent { ComponentType = typeof(MauiBlazorWebView.DeviceTests.Components.NoOpComponent), Selector = "#app", }); | ||
|
|
||
| await InvokeOnMainThreadAsync(async () => | ||
| { | ||
| var bwvHandler = CreateHandler<BlazorWebViewHandler>(bwv); | ||
| var platformWebView = bwvHandler.PlatformView; | ||
| await WebViewHelpers.WaitForWebViewReady(platformWebView); | ||
|
|
||
| var lifecycleService = MauiContext.Services.GetService<ILifecycleEventService>() as LifecycleEventService; | ||
| Assert.NotNull(lifecycleService); | ||
|
|
||
| // Verify handler is registered after connect | ||
| Assert.True(lifecycleService.ContainsEvent(nameof(AndroidLifecycle.OnBackPressed)), | ||
| "OnBackPressed handler should be registered after ConnectHandler"); | ||
|
|
||
| // Count the handlers before disconnect | ||
| var handlersBefore = lifecycleService.GetEventDelegates<AndroidLifecycle.OnBackPressed>(nameof(AndroidLifecycle.OnBackPressed)); | ||
| int countBefore = 0; | ||
| foreach (var _ in handlersBefore) | ||
| countBefore++; | ||
|
|
||
| // Disconnect the handler by setting the BlazorWebView's Handler to null | ||
| // This triggers DisconnectHandler internally | ||
| bwv.Handler = null; | ||
|
|
||
| // Count the handlers after disconnect | ||
| var handlersAfter = lifecycleService.GetEventDelegates<AndroidLifecycle.OnBackPressed>(nameof(AndroidLifecycle.OnBackPressed)); | ||
| int countAfter = 0; | ||
| foreach (var _ in handlersAfter) | ||
| countAfter++; | ||
|
|
||
| // Verify the handler count decreased (cleanup happened) | ||
| Assert.True(countAfter < countBefore, | ||
| $"OnBackPressed handler should be removed after DisconnectHandler. Before: {countBefore}, After: {countAfter}"); | ||
| }); | ||
| } | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.