Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView loadUrl works only once

Tags:

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??

like image 501
Elena Avatar asked Jan 11 '13 13:01

Elena


People also ask

Why is my WebView not working?

You might often face issues in updating the chrome and Android System Webview. To fix this problem, you can reboot your device, check your internet connection, stop auto-updating all apps, clear Google Playstore cache, and storage, leave the beta testing program, and manually update Android WebView app from Playstore.

Is Android WebView deprecated?

This interface was deprecated in API level 12. This interface is now obsolete.

Do cookies work in WebView?

By default this is set to true and the WebView accepts cookies.


2 Answers

Use exact in below sequence.

   wv.clearCache(true);    wv.clearView();    wv.reload();    wv.loadUrl("about:blank");    wv.loadData(Constants.BLOG_URL); 
like image 109
user1621629 Avatar answered Sep 27 '22 15:09

user1621629


I have spent the last day or so working on an application that utilised a WebView. I did have a few issues with loading data into the WebView.

I am not entirely sure this would work but perhaps replace this code:

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

}

with this code:

public void reLoadUrl() {

    wv.clearView();     wv.loadUrl(Constants.BLOG_URL); 

}

maybe when you clear the WebView it will solve the issue

like image 24
Javacadabra Avatar answered Sep 27 '22 15:09

Javacadabra