WebView shouldOverrideUrlLoading() not called for invalid links
android, android-webview
Solution
You're probably using the KitKat WebView. This is a known issue (I think it's outlined in the migration guide) where URLs that can't be resolved against the base URL are dropped on the floor (you won't get any callbacks for them, neither shouldOverrideUrlLoading nor onPageStarted).
The problem is that your base URL is a data url, so you're trying to resolve '/q?type=short' against 'data:text/html,...' which doesn't make much sense and so the whole attempt to navigate to the URL gets ignored.
This was different for the pre-KK WebView which used KURL instead of GURL for URL processing. GURL is generally more strict (and more secure) than KURL, which is the cause for some incompatibility between the two WebView versions.
Problem
There are two types of links in the HTML file: ``` (1) A normal link like http://www.bbb.com/q?type=normal (2) A short link like /q?type=short. ``` For the first kind, just load the url. For the second kind, I should prepend it with a fixed address like http://www.abc.com before loading the url. I am trying to do this with overriding the shouldOverrideUrlLoading() function in WebViewClient. However this function doesn't gets called for the second type of link. I tried prepending the "http://www.abc.com" to the second type of links in the HTML file. Then the function does get called when I click the second kind of link. I think what's happening is WebView will first check if the link is a valid url. Only if it is valid will the function gets called. Am I right? How can I solve this? Thanks in advance. ``` contentWebView = new WebView(context); webViewClient = new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // String not in Logger. Log.d(TAG, "Here!"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); context.startActivity(intent); return true; } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (hosted) { contentWebView.setVisibility(VISIBLE); } else { summaryTextView.setVisibility(VISIBLE); articleLinkButton.setVisibility(VISIBLE); } progressBar.setVisibility(View.GONE); } }; contentWebView.setWebViewClient(webViewClient); contentWebView.getSettings().setJavaScriptEnabled(true); contentWebView.loadData(fullString, "text/html", "utf-8"); contentWebView.setVisibility(GONE); ``` More on this: I tried changing ``` contentWebView.loadData(fullString, "text/html", "utf-8"); ``` to ``` contentWebView.loadDataWithBaseURL("http://www.abc.com", fullString, "text/html", "utf-8", null); ``` Then the function gets called. If I change the short link to a full link in the html string manually. Then the function also gets called. So I think this is probably what is happening: The WebView checks if the link URL is valid. Only when the URL is valid will the shouldOverrideUrlLoading() be called.