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条回答
等我变得足够好
2楼-- · 2019-01-22 19:53

Just make a method in which you'll flush an reinitialize whole WebView and call it from reLoadUrl().

void loadWebView() {
    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);
}

public void reLoadUrl() {
   loadWebView();
}
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-22 19:55

I faced the same issue and none of the above solution worked for me. So I fixed it by reloading WebView using JavaScript as follows:

webView.loadUrl( "javascript:window.location.reload( true )" );

Also JavaScript needs to be enabled for the WebView:

webView.getSettings().setJavaScriptEnabled(true);
查看更多
乱世女痞
4楼-- · 2019-01-22 20:03

In onCreate, instead of using wv.loadUrl(Constants.BLOG_URL); , just use wv.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);

查看更多
小情绪 Triste *
5楼-- · 2019-01-22 20:03

Call this method in onPause()

 private void pauseWebView() {
            try {
                Class.forName("android.webkit.WebView")
                        .getMethod("onPause", (Class[]) null)
                        .invoke(webview, (Object[]) null);

            } catch(ClassNotFoundException cnfe) {

            } catch(NoSuchMethodException nsme) {

            } catch(InvocationTargetException ite) {

            } catch (IllegalAccessException iae) {

            }
        }
查看更多
We Are One
6楼-- · 2019-01-22 20:05

Use exact in below sequence.

   wv.clearCache(true);
   wv.clearView();
   wv.reload();
   wv.loadUrl("about:blank");
   wv.loadData(Constants.BLOG_URL);
查看更多
你好瞎i
7楼-- · 2019-01-22 20:05

I'd suggest you try recreating the WebView every time you use it and see if that makes any difference.

First save context, layout parms and parent.

Context context = wb.getContext();
ViewGroup.LayoutParams lp = wv.getLayoutParams();
ViewGroup parent = (ViewGroup)wv.getParent();

Then stop it loading and remove the view from its parent.

wv.stopLoading();
parent.removeView(wv);

Then recreate and add it back.

wv = new WebView(context);
parent.addView(wv, lp);

That may be other properties that you'll need to restore, but that's the general idea.

查看更多
登录 后发表回答