On the shouldOverrideUrlLoading method in Android

Posted by scialom on Fri, 04 Mar 2022 10:55:46 +0100

Public Boolean shoulderoverrideurloading (WebView, string Url) is a method defined in the WebViewClient class. Some online blogs say that the function of this method is to control the opening of Url in the current WebView when the return value is true, and call the system browser or third-party browser when it is false. But in fact, this statement is wrong.

webView = (WebView) findViewById(R.id.webView);
        //WebView loads web resources
       webView.loadUrl("http://baidu.com");

When running the above code, the APP will call an external browser to open the web page.

webView = (WebView) findViewById(R.id.webView);
        //WebView loads web resources
       webView.loadUrl("http://baidu.com");
        //Override WebView's default behavior of opening web pages using a third party or the system's default browser, so that web pages can be opened with WebView
       webView.setWebViewClient(new WebViewClient(){
           @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
             view.loadUrl(url);
            return true;
        }
       });

When running the above code, the APP will access the web address in the internal WebView.

Therefore, some people think that this code completes this function, and further infer that when the return value is true, control to open WebView and call system browser or third-party browser for false.
**This is even more wrong** because

webView = (WebView) findViewById(R.id.webView);
        //WebView loads web resources
       webView.loadUrl("http://baidu.com");
        //Override WebView's default behavior of opening web pages using a third party or the system's default browser, so that web pages can be opened with WebView
       webView.setWebViewClient(new WebViewClient(){
           @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
             view.loadUrl(url);
            return false;
        }
       });

Change the return value from true to false, and it is found that the APP will still access the web address in the internal WebView.

More extreme:

webView = (WebView) findViewById(R.id.webView);
        //WebView loads web resources
       webView.loadUrl("http://baidu.com");
        //Override WebView's default behavior of opening web pages using a third party or the system's default browser, so that web pages can be opened with WebView
       webView.setWebViewClient(new WebViewClient());

This method is not rewritten at all, but a newly initialized WebViewClient is set to the default WebViewClient of the currently obtained WebView. Similarly, the APP will still access the web address in the internal WebView.

Therefore, the shouldoverrideurlloading (WebView, string URL) method is not this function at all. Some posts are purely misleading. As long as the custom WebViewClient is set, the application will change from calling the external browser to open the web address by default to opening the web address on the local WebView by default.

Check the official documentation for the use of the shouldOverrideUrlLoading method

    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if ("www.example.com".equals(Uri.parse(url).getHost())) {
                // This is my website, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
    }
    

In the code before the official example, the default WebViewClient of WebView has been set as MyWebViewClient defined above. From the functions implemented inside this method, we can see that this method is actually in WebView When the loadurl () method is executed, it will be automatically called for interception processing. The following is the official explanation:

Give the host application a chance to take control when a URL is about to be loaded in 
the current WebView. If a WebViewClient is not provided, by default WebView will ask 
Activity Manager to choose the proper handler for the URL. If a WebViewClient is 
provided, returning true causes the current WebView to abort loading the URL, while 
returning false causes the WebView to continue loading the URL as usual.
Be a URL About to be loaded into the current WebView Give the host application a chance to control. If not
 Available WebViewClient,By default, WebView Will ask Activity Manager by URL Choose the right
 Treatment method. If one is provided WebViewClient,return true Will cause the current WebView Abort loading
URL,And return false Will lead to WebView Continue loading as usual URL. 

The function realized by the official code is that only the specified web address can be directly opened on the WebView of this application (the return value is false, which means no interception, and the default operation of opening the web address after setting WebViewClient is executed). Other web addresses that do not meet the judgment conditions will be redefined (the return value of true will be intercepted) and finally opened in the external browser. The reason for this is to ensure the security of the program and prevent some strange and dangerous websites from being opened in the application.

To sum up, when you need to open the web address in WebView in the APP page, you only need to set WebView Just set webviewclient (New webviewclient()).

Topics: Java Android