How is shouldOverrideUrl called

Issue

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.

Solution

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!!

Answered By – SteelToe

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *