a lot of Android developer implement their own webview.
Although a lot of questions related to this topic are discussed on stackoverflow, here is probably a cause of this lack of clarity.
To open links within your WebView, android developer define their own webview.
-
According to http://developer.android.com/guide/webapps/webview.html returning
falseinshouldOverrideUrlLoadingshould be used, to open the url in the current Webview.
(“current” means presumably created or used lately?) -
According to http://developer.android.com/resources/tutorials/views/hello-webview.html and various blogs:
public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; }truemust me returned.
Is there a mistake in one of the developer sites? It there difference between API level?
What is the cleanest and best use of shouldOverrideUrlLoading?
An answer would help our team and a lot of other developers working with android webviews.Thanks.
In case you decide to implement WebViewClient:
If you don’t implement WebViewClient, every time you’ll ask the WebView to load an URL with the
loadUrlmethod, 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
So if you just write something like this
the URL will load inside your own WebView and not in an external web browser.
You’ll typically return
truein shouldOverrideUrlLoading when you want to modify the URL and then load the new one with anotherloadUrlcall or when you just want to avoid loading the URL and handle the request in a different way.The behavior in your example
is equivalent to
because you’re telling the WebView to avoid handling the URL loading (
return true), but you’re also making another request withview.loadUrl(url)so in fact you end up loading the URL.