Skip to content Skip to sidebar Skip to footer

Progressbar And Refresh Webview Combination Android

EDIT: I am using a ProgressBar for a Webview that works fine. Now I want to refresh my Webview, which also works. When I am trying to combine them, the Spinner doesn't end at all.

Solution 1:

It looks like you are refreshing your WebView every 20 seconds and popping up a new progress dialog when you do. Your code assumes that the old progress dialog has already been dismissed. If it hasn't, then the progress dialog just became orphaned and will remain on display forever. You can probably fix this by inserting this code:

if (progressBar != null && progressBar.isShowing()) {
    progressBar.dismiss();
}

just before the line progressBar = ....

But I wouldn't fix your code that way. You are doing a huge amount of work that you don't need to do. (For instance, you don't need to reload the same content view.) Here's my revised version:

private final Runnable m_Runnable = newRunnable() {
    publicvoidrun() {
        doRefreshingStuff(id);
        mHandler.postDelayed(m_Runnable, 20000);
    }
};

publicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //initialitions setContentView(R.layout.web4);
    webView7 = (WebView) findViewById(R.id.web_4_1);
    webView7.getSettings().setJavaScriptEnabled(true);
    webView7.getSettings().setJavaScriptEnabled(true);
    webView7.setWebViewClient(newWebViewClient() {
        publicvoidonPageStarted(WebView view, String url, Bitmap favicon) {
            if (!mProgressBar.isShowing()) {
                mProgressBar.show();
            }
        }
        publicvoidonPageFinished(WebView view, String url1) {
            if (mProgressBar.isShowing()) {
                mProgressBar.dismiss();
            }
        }
    });

    mProgressBar = newProgressDialog(this);
    mProgressBar.setTitle("example");
    mProgressBar.setMessage("...");

    mHandler = newHandler();
}

protectedvoidonResume() {
    super.onResume();
    mHandler.post(m_Runnable);
}

protectedvoidonPause() {
    super.onPause();
    mHandler.removeCallbacks(m_Runnable);
}

publicvoiddoRefreshingStuff(String id) {
    url="my url";
    webView7.loadUrl(url);
}

Note that I moved the starting of the page loading loop to onResume() and kill the looping in onPause(). You may want slightly different logic, but you should definitely stop looping somehow when your activity pauses or finishes.

Post a Comment for "Progressbar And Refresh Webview Combination Android"