I need to access the progress value in WebView -> WebChromeClient -> onProgressChanged(). The progress integer value doesn't increment from 0 to 100 but rather jumps around. Here is a sample log-output from loading one page and the associated progress numbers:
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 100
DEBUG: progress : 10
DEBUG: progress : 30
DEBUG: progress : 37
DEBUG: progress : 45
DEBUG: progress : 48
DEBUG: progress : 49
DEBUG: progress : 50
DEBUG: progress : 52
DEBUG: progress : 54
DEBUG: progress : 56
DEBUG: progress : 59
DEBUG: progress : 61
DEBUG: progress : 63
DEBUG: progress : 66
DEBUG: progress : 68
DEBUG: progress : 70
DEBUG: progress : 73
DEBUG: progress : 75
DEBUG: progress : 77
DEBUG: progress : 79
DEBUG: progress : 82
DEBUG: progress : 84
DEBUG: progress : 86
DEBUG: progress : 87
DEBUG: progress : 88
DEBUG: progress : 89
DEBUG: progress : 100
What am I doing wrong?
Code:
webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
Log.i(LOG_TAG, "DEBUG: progress : " + progress);
}
});
As @ksasq pointed out in the above comment, the initial URL caused many redirects and thus onProgressChanged() is called many times.
Best solution is to use the progress of webview and not progress(integer value from method onProgressChanged(WebView view, int progress))
psuedo code,
This solution works fine in all cases!
From my own experiments, I found that onProgressChanged can get called multiple times for the same url for the same progress value (Ex: google.com progress = 100 calls twice).
I also found that onProgressChanged will be called late for a url while you are loading another one. Timetable example:
At this point, I should mention that in my experiments I override shouldOverrideUrlLoading() to see redirects and even when I don't see redirects the above still happens. So why does this happen?
According to this answer( Why is there a noticeable delay between WebChromeClient.onProgressChanged and jquery's $(document).ready() callbacks?): because a page is loaded before the script are triggered, document ready looks for all html elements to be loaded first except images and things like that, so your script does not fire until everything has been loaded, by that time your onprogresschanged has received 100% first as it may be have a higher priority than your javascript after all devices do ask to check if js should be allowed or not so it has to go through some checks before this method is called.
So to put it all together this is what you need to do:
Code