3

I want to use window.loadUrl("Javascript:window.location.reload( true )"); to reload a webview but can't get it to perform. May I get some advice or alternative solution? I simply want the button reload to reload the webview window

Thank you, Java below:

   package com.fortunecaster.android;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Button;

    public class FcastActivity extends Activity { 

    WebView window;
    Button reload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fcast);

        window = (WebView)findViewById(R.id.webView1);
        window.getSettings().setJavaScriptEnabled(true);
        window.getSettings().setLoadWithOverviewMode(true);
        window.getSettings().setUseWideViewPort(true);
        window.setWebViewClient(new WebViewClient());
        window.setWebChromeClient(new WebChromeClient());
        window.getSettings().setAppCacheEnabled(true);


        try {
        window.loadUrl("http://www.fortunecaster.com/android-td");
        } catch (Exception e) {
            e.printStackTrace();

            //setup button
            reload = (Button)findViewById(R.id.button1);
            //set onClickListener
            reload.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    window.loadUrl("Javascript:window.location.reload( true )");

                    // TODO Auto-generated method stub

                }
            });
      }
}
Goran Horia Mihail
  • 3,536
  • 2
  • 29
  • 40
user2544501
  • 31
  • 1
  • 1
  • 2

4 Answers4

7

Just use this reload() method as,

webview.reload();
3

Maybe this is interesting for you:

They suggest just to call loadURL() again.

Community
  • 1
  • 1
Langusten Gustel
  • 10,917
  • 9
  • 46
  • 59
  • Does this actually refresh, or will it grab from the cache? – Geobits Jul 22 '13 at 19:39
  • 1
    I guess this depends on the cache mode but continue reeding @ the link i posted. :) – Langusten Gustel Jul 22 '13 at 19:58
  • Geobits, I would like this to actually refresh/reload the webview html destination all together. Jan1337z, thanks for the link. This was the actual thread where I – user2544501 Jul 22 '13 at 21:02
  • Jan1337z, the link you referred to was the one where I originally learned about this reload method. – user2544501 Jul 22 '13 at 21:10
  • Jan1337z- No. However, I did adjust the reload method as per the article: @Override public void onClick(View v) { CastActivity.this.window.loadUrl("http://www.fortunecaster.com/android-td"); I also noticed that my references to "Button" in the java code should have been "ImageButton" so I changed them as well. But I still cannot get the button to reload the webview. I just clicks with no reaction. – user2544501 Jul 25 '13 at 16:50
  • If you want you can create a gist/pastebin or what ever and I can try to solve your problem ... – Langusten Gustel Jul 26 '13 at 11:34
  • 1
    Jan1337z– Thanks. I appreciate your offer, but after doing some process of elimination in the code, I found that the problem was with the exception I set up in the loadURL. Once I removed the exception, all of the reload methods worked. I went with window.loadUrl("javascript:window.location.reload(true)"); as the content of my webview has random image loading and this method results in a true webview reload without and cache issues. – user2544501 Jul 27 '13 at 18:37
  • 1
    Just try mWebView.reload(); !! – molu2008 Mar 14 '14 at 13:46
1
@Override
            public void onClick(View v) {
                window.loadUrl(window.GetUrl());

                // TODO Auto-generated method stub

            }
        });
1

make a function to call webView, and reuse it.

Moreover, you need to use webView.reload(); in quite front of coding to refresh(make it empty) the webView page.

protected void setWebView(){
   window = (WebView)findViewById(R.id.webView1);
   window.reload(); // this make empty window, 
                    // so if you call setWebView() again, 
                    // you might rebuild new webview from here.
   window.getSettings().setJavaScriptEnabled(true);
   window.getSettings().setLoadWithOverviewMode(true);
   window.getSettings().setUseWideViewPort(true);
   window.setWebViewClient(new WebViewClient());
   window.setWebChromeClient(new WebChromeClient());
   window.getSettings().setAppCacheEnabled(true);
   window.loadUrl("http://www.fortunecaster.com/android-td");
}
김도형
  • 31
  • 6