I am working in Android Studio an I am using a WebView to handle a web page within my application. I would like to track URL redirects within this web page to allow me to move to the next activity at the correct time.
This URL tracking can be carried out by overriding the WebViewClient class method 'shouldOverrideUrlLoading' to allow me to forward to a new activity for a specific URL. However, there are two implementations of 'shouldOverrideUrlLoading':
shouldOverrideUrlLoading(WebView view, String url)
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
The first of which (method ending String url) is deprecated. The second method shown above only works beyond API level 21, when I want my application to target API level 15 and beyond.
I understand if this was just standard code (not method overriding) I could pull the API level from the Android phone and then carry out whichever method based on the retrieved level. But I am not sure how to specify which of these overloaded methods to user based on API level of the phone.
Also I get a red squiggly warning me the a call requires API level 21, but I believe this will still compile, crashing only if called below API 21?
Below are the two versions of the overridden overloaded method:
This is the deprecated method:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.equals("test")) {
return true;
}
return false;
}
});
This is the new version of the method, where the 'WebResourceRequest' is only supported in API level 21+:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if(request.getUrl().equals("test")) {
return true;
}
return false;
}
});
Is there anyway to either specify which method to use at certain API levels? As I am not sure how to carry this out without just using the deprecated method.