Search code examples
androidwebviewwebviewclient

How to alter URL loading in a webview?


The basic idea is to alter every url being loaded in a webview (for example adding/removing get parameters).

I have a custom WebViewClient, in which I have the following method :

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    String modifiedUrl = Util.someMethod(url);
    super.shouldOverrideUrlLoading(view, modifiedUrl);
}

Will it work or should I put this logic in another method, for example onPagestarted ?


Solution

  • You rather should do something like :

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(conditionForModifyingUrl){
            String modifiedUrl = Util.someMethod(url);
            view.loadUrl(modifiedUrl);
            return true;
        }
        return false;
    }
    

    Calling super.shouldOverrideUrlLoading(view, modifiedUrl) won't work because, as it is its name, this method only checks if the url should be overridden, and does not load the url at all.