onPageStart called many times and onPageFinished not called for single page

Mahendran picture Mahendran · Oct 6, 2011 · Viewed 13.3k times · Source

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!

Answer

capdragon picture capdragon · Nov 11, 2012

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();
        }                
    }              

});