Manjunath
Manjunath

Reputation: 2063

How to avoid webview links opening in the same webview

When a user clicks on a link in the webview. I want to show a dialog asking whether the user wants to view it in default browser, if he opts YES then he should be taken to default browser otherwise it shouldn't load the link at all.
But, the issue I am facing is, I could able to show the dialog inside run() of WebViewClient's overridden method shouldOverrideUrlLoading(). When the user opts YES I am doing startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(url))); to show it in default browser. But, irrespective of the user selects for YES/NO it is showing the link in webview which I want to avoid. Any suggestions appreciated...TIA

Upvotes: 0

Views: 1441

Answers (2)

Manjunath
Manjunath

Reputation: 2063

I resolved the issue, by just reloading the same URL inside shouldOverrideUrlLoading() method. Thanks for your suggestions

Upvotes: 0

Mohd Mufiz
Mohd Mufiz

Reputation: 2236

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

private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // 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;
    }
}

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

Upvotes: 1

Related Questions