简体   繁体   中英

Android WebView http links opens Chrome browser with 2 tabs to same url

I've got a WebView in my app which displays a local html file. There are links to PDF files within it which I am handling on the shouldOverrideUrlLoading() method, but by doing so, the http:// links on the page are now being loaded in the WebView and not in the browser of the device.

The code below recognises the pdf files and this works fine, but when I click on an external url, it opens two tabs in the chrome browser to the same url. How can I only open the one?

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains(".pdf")) {
            // Remove the file:///android_asset/ text in url
            String tmpUrl = url.replace("file:///android_asset/", "");
            PdfHandler pdf = new PdfHandler(mContext);
            pdf.openPdf(tmpUrl);
            return true;
        }
        else{
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
}

Thanks

The answer is to add this flag to the Activity that launches the browser:

intent.putExtra(Browser.EXTRA_APPLICATION_ID, mContext.getPackageName());

This forces the browser to always open a link in the tab created by my application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM