How to intercept url loads in WebView (android)?
android, android-webview, webview
Solution
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.
Problem
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"); ```