Guy S
Guy S

Reputation: 1424

How to detect when redirects in WebView finish?

I have gone through every related post I found here and still can't get it to work. I'm opening my WebView with a URL that has redirects (sometimes many redirects) and I want to know when the loading is really finished (URL finished loading with no more redirects).

So any suggestions?

Every post I read said to override shouldOverrideUrlLoading but no matter what I do shouldOverrideUrlLoading does not get called.

My code:

mWebView = (WebView) findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new MyWebViewClient());
mUrl = "http://cdn.something.com/something.html";
//    mUrl = "https://www.google.com";
mWebView.loadUrl(mUrl);



public class MyWebViewClient extends WebViewClient {

    public MyWebViewClient() {}

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        Log.d(TAG, "WebViewClient: shouldOverrideUrlLoading");
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        Log.d(TAG, "WebViewClient: onPageStarted:  url: " + url);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.d(TAG, "WebViewClient: onPageFinished: url: " + url);
        super.onPageFinished(view, url);
    }

}

Thank you

Upvotes: 4

Views: 7302

Answers (2)

jfcogato
jfcogato

Reputation: 3439

In my case WebViewClient wasn't showing if there was changes on the webview, I supposed that is something about the web that is been running.

I could get that information from the WebChromeClient with OnProgressChanged, I don't know if this would help people anyway, but here is the code:

webview.webChromeClient = object : WebChromeClient(){

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            if (newProgress == 100) {
                Log.d("testing", webview.getOriginalUrl())
                Log.d("testing", webview.url)
            }
        }
    }

This way I could know what is been loaded and when is finished.

Upvotes: 0

Manohar
Manohar

Reputation: 23384

you are overriding the new shouldOverrideUrlLoading method which was added in android N,

you have to override both methods to make it work (old one which is deprecated and new one)

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    final Uri uri = Uri.parse(url);
    return handleUri(uri);
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    final Uri uri = request.getUrl();
    return handleUri(uri);
}

refer this ,and onPageFinished will be called when page has finished loading

Upvotes: 3

Related Questions