Shirgill Farhan Ansari Shirgill Farhan Ansari - 3 months ago 11
Android Question

What is overridding URL loading in WebView client?

The doc says-

* Give the host application a chance to take over the control when a new
* url is about to be loaded in the current WebView. If WebViewClient is not
* provided, by default WebView will ask Activity Manager to choose the
* proper handler for the url. If WebViewClient is provided, return true
* means the host application handles the url, while return false means the
* current WebView handles the url.
* This method is not called for requests using the POST "method".
* @param view The WebView that is initiating the callback.
* @param url The url to be loaded.
* @return True if the host application wants to leave the current WebView
* and handle the url itself, otherwise return false.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;

What exactly is the difference b/w the host application handling the url opposite to when WebView handles the url?

UPDATE: Even though your words are ultra clear to me regarding the host application & WebView, this problem is further in reference to the code snippet from your book which I have purchased-

public class BrowserDemo3 extends Activity {
WebView browser;

public void onCreate(Bundle icicle) {
browser.setWebViewClient(new Callback());

loadTime(); // 1

void loadTime() {
String page=
"<html><body><a href=''>"
+ DateUtils.formatDateTime(this, new Date().getTime(),
+ "</a></body></html>";

browser.loadData(page, "text/html", "UTF-8");

private class Callback extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
loadTime(); //2


Please notice the statements marked 1 & 2 above.

1) Why loadTime() is c/d from the shouldOverrideUrlLoading(...)? I see that commenting that line seems to have no effect.

2) if true is returned from the overridden method shouldOverrideUrlLoading(...) which I think just reloads the WebView. It doesn't results in error as in case 3.

3) if false is returned from the same method, then clicking on the link shows this....Why? Is it because the WebView developers decided it to be put that way, while in other case, it will be the responsibility of the app developer(me) to deal with it.

enter image description here.


The "host application" is what you are writing. Hence, "the host application handling the url" is when you, as the developer, decide what to do with the URL.

The implementation of WebView was written by other developers. Hence, "the WebView handles the url" is when those developers decide what to do with the URL.

Classically, WebView would handle the URL by calling startActivity() on an ACTION_VIEW Intent wrapping the URL, to launch that URL in a standalone Web browser. An app developer might use shouldOverrideUrlLoading() to call loadUrl() on the WebView itself, to keep the URL within the WebView (and, hence, within the app).