In case you decide to implement WebViewClient:
webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// My own condition to decide if I should skip URL loading
boolean avoidURLLoading = ...
if (avoidURLLoading)
{
// Ask the WebView to avoid loading the URL,
// I want to manage this case on my own.
return true;
}
else
{
// Let the WebView load the URL
return false;
}
};
});
If you don't implement WebViewClient, every time you'll ask the WebView to load an URL with the loadUrl
method, it will ask the Activity Manager to find a proper app to load the URL (typically a web browser installed in the device).
The default implementation of shouldOverrideUrlLoading in WebViewClient is
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
So if you just write something like this
webView.setWebViewClient(new WebViewClient());
the URL will load inside your own WebView and not in an external web browser.
You'll typically return true
in shouldOverrideUrlLoading when you want to modify the URL and then load the new one with another loadUrl
call or when you just want to avoid loading the URL and handle the request in a different way.
The behavior in your example
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
is equivalent to
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
because you're telling the WebView to avoid handling the URL loading (return true
), but you're also making another request with view.loadUrl(url)
so in fact you end up loading the URL.