WebView loadUrl works only once

2019-01-22 19:22发布

EDIT: I worked on this project years ago and unfortunately I cannot verify if any of the answers is working in the given scenario.

I am having hard time with one WebView which should show our blog. When initilized, it works just fine. The user can navigate to different links within the WebView. To get back to the blog, there is a button outside the WebView which should load the main blog site again.

The problem is, that nothing is loaded after the second call to loadUrl. Here is my code:

private WebView wv;

        @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            this.setContentView(R.layout.blog);

    wv = (WebView) findViewById(R.id.blog_webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon {
            MyLog.logDump("onPageStarted: " + url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
                            MyLog.logDump("onPageFinished: " + url);
        }
    });

    wv.loadUrl(Constants.BLOG_URL);
}

The function called by my OnClickListener is the following:

    public void reLoadUrl() {
        wv.loadUrl(Constants.BLOG_URL);

}

But despite that the logs in onPageFinished and onPageStarted show that my wv.loadUrl is being invoked and that it's loading the correct url, the content in the webview itself doesn't change. I've tried clearing the cache, the history, the view, tried different WebSettings, tried to use webView.goBack() - not result. Also those ideas don't work: Strange webview goBack issue in android

Sometimes, the reLoadUrl shows the desired result - but once it fails it no longer can be made to work again. Any ideas what could be happening? I did try to read the WebView code but I couldn't find anything that could help me.

The only thing that I can add, is that we are using some ad networks which are heavily dependent on webViews - I tried to turn those down, but I didn't remove the libraries so I am not sure that they are not the culprit.

Any ideas??

14条回答
Ridiculous、
2楼-- · 2019-01-22 19:42

I was facing the same issue. I was creating a new Webview everytime. Just first destroy your previous webView before creating a new one. I hope this help!! Make your webview a global variable. Like -

Webview w;

then before creating a new webview just destroy the previous one

                if(null != w)
                {
                    //destroy the previous webview, before creating the new one
                    w.destroy();
                }
                w= new WebView(activity);
查看更多
淡お忘
3楼-- · 2019-01-22 19:45

Try to load a blank page before:

wv.loadUrl("about:blank");
wv.clearHistory();
wv.clearView();
wv.loadUrl(Constants.BLOG_URL);

Edit: This is a code I used before because I had problems with this too, please try it.

查看更多
老娘就宠你
4楼-- · 2019-01-22 19:48
@override
public void onFormResubmission(WebView view, Message dontResend, Message resend){
   resend.sendToTarget();
}
查看更多
Deceive 欺骗
5楼-- · 2019-01-22 19:48

I had the same issue, calling resumeTimers() solved the problem for me...

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-22 19:52

Try setting a WebChrome client. The webview needs support of WebChrome Clint to work properly in some cases.

wv.setWebChromeClient(new WebChromeClient());
查看更多
够拽才男人
7楼-- · 2019-01-22 19:52

I am facing the same issue and got it working by following steps:

webView.clearCache(true);
webView.loadUrl("Url");

and I got the multiple url loaded successfully.

查看更多
登录 后发表回答