onPageStart called many times and onPageFinished not called for single page

13,658

Solution 1

Check whether it is already open with .isShowing().

final ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");

browser.setWebViewClient(new myViewClient(){

    @Override  
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

        if(!mProgressDialog.isShowing())
        {
            mProgressDialog.show();
        }

    }  
    @Override  
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        if(mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }                
    }              

});

Solution 2

If you have ajax calls in the page being loaded, onPageFinished() will be called only when those calls finish. Better to make those calls with a window.setTimeout(). In that case, UI thread will not be blocked on those calls and onPageFinished() will be called as soon as the main page loads.

Share:
13,658
Mahendran
Author by

Mahendran

Android - Xamarin Forms developer

Updated on June 25, 2022

Comments

  • Mahendran
    Mahendran almost 2 years

    My question is different from this one guys.. I wany my progress dialog start when page load starts and end when the page load finished in my webview. My problem is the progress dialog starts and never get dismissed.I have set break points it shows that the progress dialog starts and get dismissed many times then it starts and not get dismissed even after page load completed. My question is why the onPageStarted getting executed many time for a single page loading? and why onPageFinished not called after completion of page load?

           myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                myWebView.loadUrl(url);
                return true;
            }
    
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(myWebView, url, favicon);
                Log.d("mytag","Page Loading Started");
                //myURLProgressDialog= ProgressDialog.show(WebviewExampleActivity.this, "Page Loading", "Wait for a moment...");
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.d("mytag","Page Loading Finished!");
                super.onPageFinished(myWebView, url);
                //myURLProgressDialog.dismiss();
            }
    
        });
    

    My self tagged filtered Log is Like this for loading single page:

       10-06 10:32:49.298: DEBUG/mytag(508): Page Loading Started
       10-06 10:32:49.998: DEBUG/mytag(508): Page Loading Started
       10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Finished!
       10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Started
       10-06 10:33:00.898: DEBUG/mytag(508): Page Loading Finished!
    

    When I am clicking link on already loaded page it works fine. Here is Log:

    10-06 10:59:25.098: DEBUG/mytag(543): Page Loading Started
    10-06 10:59:30.889: DEBUG/mytag(543): Page Loading Finished!
    
  • ComDubh
    ComDubh over 8 years
    This worked for me (thanks!) but it still seems odd that each onPageStarted invocation does not seem to have a corresponding on PageFinished invocation.
  • Amit Hooda
    Amit Hooda over 6 years
    window.setTimeout() can you explain how to do this ?
  • c0dehunter
    c0dehunter over 5 years
    How to do this?