For some reason the onPageFinished is firing before the WebView has finished loading - I can't figure out why...
public class WebViewClientTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);开发者_Go百科
setContentView(R.layout.main);
final WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webview, url);
webview.scrollTo(0, 500);
}
});
webview.loadUrl("http://www.google.co.uk/search?sourceid=chrome&ie=UTF-8&q=lala");
}
}
OK, well it looks like this isn't fixed. I think there's a race condition going on when loading the page, but can't get a reproducible behaviour.
I'm storing the HTML content of a webpage in a SQLite database for viewing when offline. I reload the content into the WebView with:
webView.loadDataWithBaseURL("fake://fake.com/", htmlBody, "text/html", "utf-8", null);
It seems that sometimes when the WebView loads it fires the WebViewClient.onPageFinished() method correctly, and other times it does not. Sometimes it appears to fire before the page has finished loading, producing a contentHeight of 0 and ignoring any scrollTo calls.
Anyone have any experience with this?
I had a project that had code which needed to run only after the webview had displayed it's content, and like you, onPageFinished() wasn't working. It fired too quickly, before the webview had actually rendered the page.
Instead, I had to use a "PictureListener" which gets fired when the webview actually updates the screen.
You use it like so:
mWebView.setPictureListener(new MyPictureListener());
//... and then later on....
class MyPictureListener implements PictureListener {
@Override
public void onNewPicture(WebView view, Picture arg1) {
// put code here that needs to run when the page has finished loading and
// a new "picture" is on the webview.
}
}
I had the same problem to dismiss my progressdialog when rendering my web page. I solved with onPageStarted. I hope this solution can help you.
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.dismiss();
}
@Override
public void onPageStarted(WebView view, String url,
android.graphics.Bitmap favicon) {
if (!progressBar.isShowing()) {
progressBar.show();
}
};
Hmm, well if I make my own private class, it works...
private class ViewStoryWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(webView, url);
webView.scrollTo(0, 50);
}
}
I'm getting the exact same thing with loadDataWithBaseURL()
. Sometimes the contents don't show up and the contentHeight is 0. The odd thing is that clicking anywhere in the web view will cause the contents to show. Also, if you call zoomIn();zoomOut();
in the onPageFinished()
method, the page will appear.
EDIT: The problem with my code is that the HTML content had a dummy window.location= assignment in the onload event, and this was causing the original content not to load at random times. Sorry, I don't think this is related to what you are seeing.
I was literally looking for an answer for the past 6 hours and finally found one that works for me at least, and it is a simple answer. A timer. After your OnPageFinished start a timer for as long as you want, in my case I did it for 2 seconds. This will be enough time for the webview to render and load the entire page. Make sure you run the timer on the UI thread if you want to call a webview method
Timer myTimer = new Timer(); //global variable
public void startTimer(TimerTask t){
myTimer.scheduleAtFixedRate(t,2000,2000);
}
....
public void onPageFinished(WebView view, String url) {
TimerTask task = new TimerTask() {
@Override
public void run() {
//stuff to do after page is fully loaded
myTimer.cancel();
}
};
startTimer(task);
}
By the way, this is my first answer on stackoverflow. Hope this helps someone!
精彩评论