What is considered onPageFinished in the Android WebViewClient?

android, webview, webviewclient

Solution

You need to implement the callbacks from WebChromeClient. The onPageFinished() is an API that is provided by WebViewClient. There is yet another interface named WebChromeClient that provides the progress information you are seeking:

http://developer.android.com/reference/android/webkit/WebChromeClient.html#onProgressChanged(android.webkit.WebView, int)

Open the link above and look for onProgressChanged(WebView view, int newProgress) - the 'newProgress' variable gives you the percentage of page load that was completed. When it reaches 100 you have a valid page. onPageFinished() cannot be reliably used for this (due to server side redirections etc)

I don't know what you mean by "when JS on the page is finished". Maybe you can clarify what you meant?

Problem

I am trying to perform an action on an Android WebView after my webpage finishes loading in it. I setup my WebView to have a WebViewClient to make use of the onPageFinished event callback. However, after some testing, it does't seem to wait until all the JS on the page is done loading before my onPageFinished code fires. The Google documentation says this: ``` public void onPageFinished (WebView view, String url) ``` Added in API level 1 Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture). Parameters view The WebView that is initiating the callback. url The url of the page. 1) Does onPageFinished only wait for the DOM to load? 2) Is there a way to detect when any JS on the page finishes? If so, what should I use? I don't see anything in WebViewClient that would be for that purpose. I don't want to add a delay since my users can be on EDGE or on LTE.

Original source