Sijmen Mulder
Sijmen Mulder

Reputation: 5819

Merely setting webViewClient causes links to be opened inside webview

Normally, when a link is clicked inside a WebView, an intent causes the web browser or another activity supporting that intent to open.

WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("http://google.com");

However, when I merely set an empty WebViewClient…

WebView webView = new WebView(this);
webView.setWebViewClient(new WebViewClient() {});
setContentView(webView);
webView.loadUrl("http://google.com");

…links are opened within the WebView and no intents are fired! How come?

Upvotes: 2

Views: 161

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

This is by design.

http://developer.android.com/guide/webapps/webview.html#HandlingNavigation

To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). For example:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());

That's it. Now all links the user clicks load in your WebView.

And

http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)

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

shouldOverrideUrlLoading defaults to returning false

Upvotes: 2

Related Questions