Yawar
Yawar

Reputation: 1943

Refresh webview when user open wifi and back to app

I am loading url in webview, but if wifi is off I am opening WIFI setting.

Now I want when user turn on wifi and come back webview should refresh it self to load url again.

And is there a way to refresh it continuously after 5 seconds only if webpage is not loaded due to any reason.

Upvotes: 0

Views: 537

Answers (1)

Arpan Sharma
Arpan Sharma

Reputation: 2162

You can check in the onResume() method if the user has returned back with wifi on and then call his method

@Override
    public void onResume() {
        super.onResume();
        if(connected){
            webview.loadUrl("your url");
        }
    }

For refreshing it every 5 sec you can use timer

Handler ha=new Handler();
ha.postDelayed(new Runnable() {
    @Override
    public void run() {
        webview.loadUrl("your url");
        ha.postDelayed(this, 5000);
    }
}, 5000);

I have just written connected in the if condition.You will have to use the way you are checking the connectivity.

wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
    }
 });

You can keep a boolean flag to check if the url loaded with error and change its value in onREceived error. and load new url only if it had error the previous time.

Upvotes: 1

Related Questions