11

I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.

My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.

How can I prevent the WebView to load the url?

Here's my code :

WebView banner = ... banner.setWebViewClient(new WebViewClient() { @Override public void onLoadResource(WebView view, String url) { if (url.startsWith("app://")) { Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class); //startActivity(i); } } } banner.loadUrl("url_to_the_banner"); 

2 Answers 2

24

Use WebViewClient.shouldOverrideUrlLoading instead.

public boolean shouldOverrideUrlLoading(WebView view, String url){ // handle by yourself return true; } 

WebViewClient Reference

Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

if the tap in Webview is a link to custom URL, (like myurl://parameters), its not getting called. How to resolve this problem?
7

But it must return false otherwise, so:

@Override public boolean shouldOverrideUrlLoading(WebView view, String url){ if(url.startsWith(myString){ // handle by yourself return true; } // ... return false; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.