Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override overloaded methods, where one is deprecated, for backwards compatibility?

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.

like image 243
Calco Avatar asked Dec 13 '16 11:12

Calco


People also ask

Can we override the overloaded methods?

So can you override an overloaded function? Yes, since the overloaded method is a completely different method in the eyes of the compiler. Overriding isn't the same thing at all.

Can methods be overloaded multiple times?

Introduction. Method overloading is a salient feature in Object-Oriented Programming (OOP). It lets you declare the same method multiple times with different argument lists.

Can you overload a method in a subclass?

Note: In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass instance methods—they are new methods, unique to the subclass.


Video Answer


2 Answers

You can just put a condition checking android version.

how-can-i-check-the-system-version-of-android

Summary:

if (android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.LOLLIPOP) { // only for LOLLIPOP and newer versions }

Always try to use newer api's as they tend to remove bugs but if you want old deprecated methods you can always put condition.

like image 177
Aman Kapoor Avatar answered Oct 06 '22 22:10

Aman Kapoor


Just create a method that both call.

Something like this

    public override bool ShouldOverrideUrlLoading(WebView view, string url)
    {
        return HandleUrlLoading(view,url);
    }

    public override bool ShouldOverrideUrlLoading(WebView view, IWebResourceRequest request)
    {
        string url = request.Url.ToString();
        return HandleUrlLoading(view, url);
    }

    private bool HandleUrlLoading(WebView view, string url)
    {
        if(url.equals("test")) {
            return true;
        }
        return false;
    }
like image 33
Derek Lawrence Avatar answered Oct 07 '22 00:10

Derek Lawrence