Android- Webview onPageFinished Called Twice
So, none of the above answers gave me a solution, because my problem wasn't redirecting, the webview simply called onPageFinished 3 times on the same webpage. My solution is as below, the main idea is to perform your functions on the same url only once.
private String urlFinished = " ";
@Override
public void onPageFinished(WebView view, String url) {
Logger.d(TAG, "onPageFinished");
if (!urlFinished.equals(url)) {
//Place your code here
}
urlFinished = url;
super.onPageFinished(view, url);
}
If the url is OK after onPageStarted method starts onPageFinished, but if the url is redirected after onPageStarted starts shouldOverrideUrlLoading and then onPageFinished. You should just check if the loading URL is redirected or not
private boolean isRedirected;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (!isRedirected) {
//Do something you want when starts loading
}
isRedirected = false;
}
If the URL is redirected the callback starts this function
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
isRedirected = true;
return true;
}
Before doing something in onPageFinished check if the callback has entered into shouldOverrideUrlLoading method
@Override
public void onPageFinished(WebView view, String url) {
if (!isRedirected) {
//Do something you want when finished loading
}
}
Maybe it helps someone.
I have the some problem - method onPageFinished called twice.
webView.getProgress();
at the first execution webView.getProgress() == 89, and at the second == 100. 100 means page loading complete.