Question

In my Android app I have a WebView element with a custom WebViewClient. It shows an error page via onReceivedError(), and it returns false for shouldOverrideUrlLoading().

I would expect the WebView now to open links in the external browser, not in the WebView itself, as it is explained in many other posts on SO. However, this does not work. It still opens every link inside the WebView.

Has it maybe something to do with the fact that I don't load the content via loadUrl() but via loadData() and pass an html-string?

Was it helpful?

Solution

You must use an intent to launch the url in the external browser. Method shouldOverrideUrlLoading() is a good candidate:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Context context = view.getContext();
    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

    // Launch default browser
    context.startActivity(browserIntent);

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top