How to handle the shouldInterceptRequest parameter in API21?

In API21 Google, the mustInterceptRequest method has been changed to use WebResourceRequest request instead of String url . Is there a way to write a generic class extending WebViewClient and handle both methods? My minimum version of the API is 18.

Thanks Krystian

+6
source share
1 answer

Google has modified the mustInterceptRequest method to use a WebResourceRequest request instead of a string URL

No, they added a second shouldInterceptRequest() method. Both are available in API Level 21+; String option is available at API level 11+. Although the String character is deprecated, the String variant has to be supported for quite some time for backward compatibility.

Is there any way to write a general class extending WebViewClient and handle both methods?

The built-in implementation of the WebResourceRequest version of shouldInterceptRequest() simply calls the implementation of String shouldInterceptRequest() :

 public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { return shouldInterceptRequest(view, request.getUrl().toString()); } 

(from source right now)

So, you have two options:

  • Just override the String version if you do not need WebResourceRequest and it will be used at all relevant API levels.

  • Cancel both parameters, knowing that WebResourceRequest will be used at API level 21+, and String version will be used at API levels 11-20.

+13
source

All Articles