How is shouldOverrideUrl called

1,290

The WebView calls these methods itself when a page is supposed to be loaded.

So let us say the WebView is trying to load www.google.com, the method shouldOverrideUrlLoading will get a call with the web address www.google.com passed in to the request argument.

You can return true or false to tell the WebView whether it should load the url, or if it should stop it.

A common use case is to prevent the user from navigating away from a specific webpage in a in app browser.

Hope this helps!!

Share:
1,290
Carrie
Author by

Carrie

Updated on December 14, 2022

Comments

  • Carrie
    Carrie over 1 year

    I'm totally new to android development. Recently I'm asked to investigate something about webview loading for our app which is written by flutter, and used flutter_webview_plugin.

    After I version up flutter_webview_plugin, I find there are some changes.

    And there is the code in flutter_webview_plugin

     @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Map<String, Object> data = new HashMap<>();
            data.put("url", url);
            data.put("type", "startLoad");
            FlutterWebviewPlugin.channel.invokeMethod("onState", data);
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Map<String, Object> data = new HashMap<>();
            data.put("url", url);
    
            FlutterWebviewPlugin.channel.invokeMethod("onUrlChanged", data);
    
            data.put("type", "finishLoad");
            FlutterWebviewPlugin.channel.invokeMethod("onState", data);
    
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            // returning true causes the current WebView to abort loading the URL,
            // while returning false causes the WebView to continue loading the URL as usual.
            String url = request.getUrl().toString();
            boolean isInvalid = checkInvalidUrl(url);
            Map<String, Object> data = new HashMap<>();
            data.put("url", url);
            data.put("type", isInvalid ? "abortLoad" : "shouldStart");
    
            FlutterWebviewPlugin.channel.invokeMethod("onState", data);
            return isInvalid;
        }
    

    I tried to search everywhere by using shouldOverrideUrlLoading, onPageStarted,onPageFinished, but can't find where they are called. I think they should be used by like:

    BrowserClient webViewClient;
    webviewClient.shouldOverrideUrlLoading() 
    

    or

    webViewClient.invokeMethod('shouldOverrideUrlLoading',arg)
    

    something like above. But I can't find anything.

    • SteelToe
      SteelToe over 4 years
      The webview calls these methods itself, by implementing it and returning a true / false value, you can decide whether to allow the webview to load a specific url or not
    • SteelToe
      SteelToe over 4 years
      a common use case to is to prevent the user from navigating away from your site
    • Carrie
      Carrie over 4 years
      Oh, got it, thank you @SteelToe