I have an app that displays a splash page and removed that splash page when a URL is loaded in WebView. The following is the relevant code we are using to remove the splash page:
browser.setWebViewClient(new BrowserClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Work around for WebView onPageFinished called twice
if (flag == true) {
browser.setVisibility(View.VISIBLE);
splashImage.setVisibility(View.INVISIBLE);
pageLoader.setVisibility(View.INVISIBLE);
} else {
flag = true;
}
}
});
This code works... except it is slow. The splash page takes far too long to remove, long after the webpage has loaded.
Are there any tips on how I can reliably detect when WebView has loaded a page? I've been researching this for the past few days and I can't seem to come up with anything that is reliable.
The most promising I saw is the following, but putting this code throws an error in Android Console:
@Override
public void invalidate() {
super.invalidate();
if (getContentHeight() > 0) {
// WebView has displayed some content and is scrollable.
}
}
Thanks!
EDIT: There are a lot of answers proposing onPageFinished, and even someone marking this as a duplicate with a link to solutions using onPageFinished. Folks, we already are using onPageFinished. We are looking for an alternative to onPageFinished due to how unreliable it is.