When i tap a hyperlink with target="_blank"
attributes
, WebChromeClient#onCreateWindow
is called but i cannot find a way to now what url the new window will open? host page url is the only thing i can know?
I want to change the app behavior according to the target url. any help is appreciated, thank you!
i could get clicked url by calling like as follows
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
WebView.HitTestResult result = view.getHitTestResult();
int type = result.getType();
String data = result.getExtra();
// do something
}
Maybe I am late to the party but none of the above solution worked for me on Android 10. So here's how I did it:
@Override
public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg) {
WebView newWebView = new WebView(view.getContext());
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(newWebView);
resultMsg.sendToTarget();
newWebView.setWebViewClient(new WebViewClient() {
@SuppressLint("NewApi")
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return shouldOverrideUrlLoading(view, request.getUrl().toString());
}
@Override
public boolean shouldOverrideUrlLoading(WebView View, String url) {
if (url != null && !url.isEmpty()) {
// HERE IS URL WHICH YOU WANT
}
return false;
}
});
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With