Question

When I first create the activity, everything goes fine. However, after I choose from menu to change some text of the String values and set the webview by

webview.loadData(result, "text/html; charset=UTF-8", null);
webview.loadData(result, "text/html; charset=UTF-8", null);

I have to do it twice, or the webview will keep unchanged. Is there anyone knows what happens here? Since the result String is the same, why webview force me to loadData twice?

Was it helpful?

Solution

Avoid WebView#loadData(String data, String mimeType, String encoding) - it's buggy.

Use WebView#loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) instead.

So your instruction will be like:

webview.loadDataWithBaseURL(null,result,"text/html", "utf-8", null);

OTHER TIPS

Don't know what's your problem but looking at the webview documentation, you are using the loadData method wrongly :

Webview:loadData documentation

You probably should call your webview like this :

webview.loadData(result, "text/html", "UTF-8");

Don't know if it will solve your issue at all.

Yes with loadDataWithBaseURL it does refresh the data, but then it ignores the CSS body background-color! ... At least it can't parse "%23000000" which works with loadData.

I am loading local HTML data into my webview, and this webview is inside recyclerview, When I try webview.loadData() when it renders 1st time it working fine, but when I scrolling upward downward every inflated webview`s get messed-up. When I try second webview.loadDataWithBaseURL() its working like charm.

so,when you're loading the HTML locally and it references assets such as images & css which are also packaged locally use webview.loadDataWithBaseURL()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top