Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/BlazorWebView/src/Maui/Android/BlazorWebViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,37 @@ protected override AWebView CreatePlatformView()
return blazorAndroidWebView;
}

private const string AndroidFireAndForgetAsyncSwitch = "BlazorWebView.AndroidFireAndForgetAsync";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not to bikeshed, but would "BlazorWebView.AndroidFireAndForgetDisposeAsync" be more targeted toward the specific change in behavior?


private static bool IsAndroidFireAndForgetAsyncEnabled =>
AppContext.TryGetSwitch(AndroidFireAndForgetAsyncSwitch, out var enabled) && enabled;

protected override void DisconnectHandler(AWebView platformView)
{
platformView.StopLoading();

if (_webviewManager != null)
{
// Dispose this component's contents and block on completion so that user-written disposal logic and
// Blazor disposal logic will complete.
_webviewManager?
// Dispose this component's contents so that user-written disposal logic and Blazor disposal logic will complete.

// Start the disposal...
var disposalTask = _webviewManager?
.DisposeAsync()
.AsTask()
.GetAwaiter()
.GetResult();
.AsTask()!;

if (IsAndroidFireAndForgetAsyncEnabled)
{
// If the app is configured to fire-and-forget via an AppContext Switch, we'll do that.
disposalTask.FireAndForget();
}
else
{
// Otherwise by default, we'll synchronously wait for the disposal to complete. This can cause
// a deadlock, but is the original behavior.
disposalTask
.GetAwaiter()
.GetResult();
}

_webviewManager = null;
}
Expand Down