Tuesday, 6 November 2018

Android WebView new window null URLs on relative links

I have implemented a custom WebView with setSupportMultipleWindows() enabled. As such, I also have a custom WebChromeClient that overrides onCreateWindow()

For most uses, I was using the following snippet to retrieve the URL for a page in a new window (i.e. opened via target:_blank):

Message href = view.getHandler().obtainMessage();
view.requestFocusNodeHref(href);

String url = href.getData().getString("url");

There's null checks in place but I've removed them in the snippet above for simplicity's sake. Now the issue is that sometimes the data Bundle (returned via getData()) has a null URL for relative links (i.e. href="/somepage" instead of href="https://www.example.com/somepage/").

So I searched on SO and found another possible solution:

WebView.HitTestResult result = view.getHitTestResult();
String url = result.getExtra();

However, that also returns null. If I get the type of the data using result.getType() it returns 0, which maps to UNKNOWN_TYPE.`

I am unsure why it is returning null for the aforementioned methods. N.B. that if I disable support of multiple windows, those same links work just fine. Disabling such support, however, is not an option. Is there another way to get the URL from within onCreateWindow()?



from Android WebView new window null URLs on relative links

No comments:

Post a Comment