Android WebView does not start standard browser on link click - shouldOverrideUrlLoading has no effect

StackOverflow https://stackoverflow.com/questions/14799445

Вопрос

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?

Это было полезно?

Решение

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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top