On Android in .NET MAUI, a blank WebView plus ShouldOverrideUrlLoading not being hit usually indicates that the native WebViewClient/handler isn’t wired the way it was in the old renderer model, or that navigation is being short‑circuited by always returning true.
Key points and fixes:
- Do not always return
true from ShouldOverrideUrlLoading
Returning true tells Android: “this navigation is handled manually; do not continue with the default load.”
In the current code:
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
string url = request.Url.ToString();
if (_headerParams.Count > 0)
{
view.LoadUrl(url, _headerParams);
}
else
{
view.LoadUrl(url);
}
return true; // <- blocks default navigation
}
If this method is not called for the initial navigation, the WebView never loads anything and stays white. When it is called and you also call LoadUrl from inside it, returning true can cause re‑entry and loops.
Typical pattern:
- Return
false when the URL should be handled by the WebView itself.
- Return
true only when handling the URL externally (e.g., opening system browser) and not calling LoadUrl again on the same WebView.
For header injection, consider:
public override bool ShouldOverrideUrlLoading(Android.Webkit.WebView view, IWebResourceRequest request)
{
var url = request.Url.ToString();
if (_headerParams.Count > 0)
{
view.LoadUrl(url, _headerParams);
return true; // handled once, do not re‑enter
}
return false; // let WebView proceed normally when no headers
}
Then ensure the first navigation is either:
- Started with
LoadUrl(url, _headerParams) from outside ShouldOverrideUrlLoading, or
- Allowed to proceed with
return false so the WebView can render.
- Avoid calling
ShouldOverrideUrlLoading manually
Calling this method from OnPageStarted causes the infinite loop:
-
LoadUrl → triggers navigation → OnPageStarted → calls ShouldOverrideUrlLoading → LoadUrl again → repeat.
ShouldOverrideUrlLoading is a callback for the WebView; it should not be invoked manually. Remove any manual calls from OnPageStarted or other events.
- Ensure the custom client/handler is actually attached
In MAUI handlers, the custom Android WebViewClient must be set on the native control inside the handler’s ConnectHandler/OnConnect equivalent. If the default client is still in place, the override will never fire and the WebView stays blank.
Verify that:
- The custom handler is registered for
WebView in MauiProgram.
- Inside the handler,
PlatformView.SetWebViewClient(new YourCustomClient(...)); (or equivalent) is called so that ShouldOverrideUrlLoading belongs to the active client.
- Respect MAUI/WebView lifecycle on Windows (if testing cross‑platform)
On Windows, WebView2 is stricter about when navigation can start. If the same page is blank on Windows when created dynamically, ensure the WebView is fully attached to the visual tree before setting Source or calling LoadUrl, for example by deferring navigation until after the control is loaded.
- Check for
_blank/multiple windows behavior
If the target URL uses links with target="_blank", Android WebView in MAUI will not navigate unless OnCreateWindow is implemented or multiple windows are disabled. To force links to open in the same WebView, disable multiple windows in the handler:
#if ANDROID
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("SupportMultipleWindows", (handler, view) =>
{
handler.PlatformView.Settings.SetSupportMultipleWindows(false);
});
#endif
This prevents navigation attempts that would otherwise fail silently and appear as a white screen.
Summary of changes to try:
- Remove any manual calls to
ShouldOverrideUrlLoading (especially from OnPageStarted).
- Only return
true from ShouldOverrideUrlLoading when explicitly handling the navigation and not re‑calling LoadUrl for the same request; otherwise return false.
- Ensure the custom
WebViewClient is actually set on the native WebView in the MAUI handler.
- If links use
target="_blank", disable multiple windows as shown above.
References: