Is “shouldOverrideUrlLoading” really deprecated? What can I use instead?

I have a question/problem, is “shouldOverrideUrlLoading” really deprecated? If so, what can I use instead?

It seems like shouldOverrideUrlLoading is deprecated targeting Android N and I need to make an app work since API 19 until the latest right now which is Android N (beta), I use some features that are new in Android N (like Data Saver), so targeting Marshmallow will not help with the issue since I need to use those new features, here is the part of the code I use:

  • getDatabase called recursively
  • What android:inputType should I use for entering an IP Address?
  • ViewPager setCurrentItem freezes UI thread
  • Facebook integration in android?
  • Why is Eclipse and ADB not recognizing my Android Device?
  • Using a different manifestPlaceholder for each Build Variant
  • public boolean shouldOverrideUrlLoading(WebView webview, String url) {
        if (url.startsWith("http:") || url.startsWith("https:")) {
            ...
        } else if (url.startsWith("sms:")) {
            ...
        }
        ...
    }
    

    And this is the message Android Studio gave me:

    Overrides deprecated method in ‘android.webkit.WebViewClient’
    This inspection reports where deprecated code is used in the specified inspection scope.

    Google says nothing about that deprecation.

    I wonder if using @SuppressWarnings("deprecation") will let me work on all devices since the API 19 until the latest Android N Beta (and its final version when it gets released), I can’t test it myself, I never used that and I need to be sure that it works, so, anyone can tell?

    Thanks a lot.

    Related posts:

    Android: Generate random color on click?
    Why Use Start Activities?
    Release AudioRecord android when other app request for recording
    Android Eclipse - Could not find *.apk
    Detect and handle needing to login for wifi
    Issue scrolling WebView in NestedScrollView Android 2.3 or less
  • onRequestPermissionsResult not being called in dialog fragment
  • When To Unregister Broadcast Receiver registered in onCreate?
  • How to set an arbitrary transform matrix to a View?
  • How to change spaces to underscore and make string case insensitive?
  • Use smartphone camera from a web app to scan QR code
  • Disable Scrolling in child Recyclerview android
  • 3 Solutions collect form web for “Is “shouldOverrideUrlLoading” really deprecated? What can I use instead?”

    the version I’m using I think is the good one, since is the exact same as the Android Developer Docs, except for the name of the string, they used “view” and I used “webview”, for the rest is te same

    No, it is not.

    The one that is new to the N Developer Preview has this method signature:

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
    

    The one that is supported by all Android versions, including N, has this method signature:

    public boolean shouldOverrideUrlLoading(WebView view, String url)
    

    so why should I do to make it work on all versions?

    Override the deprecated one, the one that takes a String as the second parameter.

    Documenting in detail for future readers:

    The short answer is you need to override both the methods. The shouldOverrideUrlLoading(WebView view, String url) method is deprecated in API 24 and the shouldOverrideUrlLoading(WebView view, WebResourceRequest request) method is added in API 24. If you are targeting older versions of android, you need the former method, and if you are targeting 24 (or later, if someone is reading this in distant future) it’s advisable to override the latter method as well.

    The below is the skeleton on how you would accomplish this:

    class CustomWebViewClient extends WebViewClient {
    
        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            final Uri uri = Uri.parse(url);
            return handleUri(uri);
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            final Uri uri = request.getUrl();
            return handleUri(uri);
        }
    
        private boolean handleUri(final Uri uri) {
            Log.i(TAG, "Uri =" + uri);
            final String host = uri.getHost();
            final String scheme = uri.getScheme();
            // Based on some condition you need to determine if you are going to load the url 
            // in your web view itself or in a browser. 
            // You can use `host` or `scheme` or any part of the `uri` to decide.
            if (/* any condition */) {
                // Returning false means that you are going to load this url in the webView itself
                return false;
            } else {
                // Returning true means that you need to handle what to do with the url
                // e.g. open web page in a Browser
                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                return true;
            }
        }
    }
    

    Just like shouldOverrideUrlLoading, you can come up with a similar approach for shouldInterceptRequest method.

    Use

    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.