Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPageStart called many times and onPageFinished not called for single page

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!
like image 569
Mahendran Avatar asked Oct 06 '11 04:10

Mahendran


1 Answers

Here is a solution. Instead of a loading dialog, i use another webview as splash-screen, but you can change it easily.

The trick is, to look if there is a new "onpagestart" right after the "onpagefinished". If this is the case, don't close the loading and wait for the next "onpagefinished".

myWebView.setWebViewClient(new WebViewClient() {
    boolean loadingFinished = true;
    boolean redirect = false;

    long last_page_start;
    long now;

    // Load the url
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!loadingFinished) {
            redirect = true;
        }

        loadingFinished = false;
        view.loadUrl(url);
        return false;
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        Log.i("p","pagestart");
        loadingFinished = false;
        last_page_start = System.nanoTime();
        show_splash();
    }

    // When finish loading page
    public void onPageFinished(WebView view, String url) {
        Log.i("p","pagefinish");
        if(!redirect){
            loadingFinished = true;
        }
        //call remove_splash in 500 miSec
        if(loadingFinished && !redirect){
            now = System.nanoTime();
            new android.os.Handler().postDelayed(
                    new Runnable() {
                        public void run() {
                            remove_splash();
                        }
                    },
                    500);
        } else{
            redirect = false;
        }
    }
    private void show_splash() {
        if(myWebView.getVisibility() == View.VISIBLE) {
            myWebView.setVisibility(View.GONE);
            myWebView_splash.setVisibility(View.VISIBLE);
        }
    }
    //if a new "page start" was fired dont remove splash screen
    private void remove_splash() {
        if (last_page_start < now) {
            myWebView.setVisibility(View.VISIBLE);
            myWebView_splash.setVisibility(View.GONE);
        }
    }

});
like image 129
EscapeNetscape Avatar answered Nov 14 '22 17:11

EscapeNetscape