Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shouldOverrideUrlLoading return method

I am using this method - and returning true or super.shouldOverrideUrlLoading(view,url); my apologies for being naive but I didn't understand what is the difference in returning true or the super class method?

 @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            UltimatixTouchWebView webView = (UltimatixTouchWebView) view;

            if (null != url && ((url.endsWith(".js") || url.endsWith(".css")))
                    && (checkResource(url))) {
                return true;
            } else {
                return super.shouldOverrideUrlLoading(view, url);
            }
        }
like image 476
Darpan Avatar asked Dec 01 '22 21:12

Darpan


1 Answers

I spent some time and did some permutation on all the scenerios. This is what I found -

  1. return false -> if you use this, you don't even need to load the url, ie no need to put loadUrl(url). WebView will automatically load url.

  2. return true -> Current URL will not be loaded in WebView, Quoting the Android site

    If WebViewClient is provided, return true means the host application handles the url

So, your app will handle it. ie. your app has to have some functionality to work on that url. Even if you only want to load the page back in your WebView, you will have to write webView.loadUrl(URL);. Otherwise it will not load your page.

In this example, suppose you want to go to second.html from first page.

public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(url.contains("second.html")){
                    Toast.makeText(con, "Second Page", Toast.LENGTH_LONG).show();
                    view.loadUrl(url);
                    }
                return true;
}

Here, if you click any link on second page, it will not go anywhere. Because in this function your if condition does not fulfill, it returns true so it will see if app has implemented something. Since we have not, so it will stay there only.

I tried to simplify it to help out new developers.

like image 104
Darpan Avatar answered Dec 03 '22 12:12

Darpan