Skip to content
Advertisement

Is there a way to refresh webView every x seconds?

Now I have this function for reload webView:

    public void reloadWebView() {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                webView.reload();
            }
        }, 5000);}

And it is called on onCreate function

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        reloadWebView();


    }

The function works but it only runs once

Advertisement

Answer

Easy to solve as long as the Activity is running in foreground:

 Handler handler = new Handler();
 public void reloadWebView() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            webView.reload();
            reloadWebView();
        }
    }, 5000);}

Note that handler is now a field, out of reloadWebView(). Now call it in your onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    reloadWebView();
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement