18

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?

Owen Zhao
  • 3,205
  • 1
  • 26
  • 43
  • I'm having exactly the same issue. Have you found a way to fix it? – Javide Sep 02 '13 at 22:47
  • No, I just load it twice. As it is better than not showing the right thing. I think maybe it is related to cache. So if you have time, please try `public void clearCache (boolean includeDiskFiles)` or `public void clearHistory ()` and see if it works. I just don't have time to test it as I am busy doing something else. So please tell me if they work or not. Thank you. – Owen Zhao Sep 02 '13 at 23:57
  • I found the problem that was affecting my case. My WebView is a subview of a ViewSwitcher. I embedded the WebView in a LinearLayout and this is what was causing the WebView to not display the first time it was loaded. Removing the LinearLayout fixed the issue. – Javide Sep 03 '13 at 00:13
  • Thank you. But I don't have a layout in my case. `public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); webview = new WebView(this); webview.getSettings().setBuiltInZoomControls(true); setContentView(webview);` – Owen Zhao Sep 03 '13 at 01:00

4 Answers4

41

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);
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
1

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.

Benoit
  • 1,168
  • 8
  • 16
  • 1
    Sorry, your answer is not even close. Just go to google translate and write something in your own language and then translate them to Chinese or Japanese. Then copy those characters to your project and compare our codes and you will find my webview is the same as the google translate and yours is with wrong characters in. – Owen Zhao Jul 11 '13 at 02:47
  • Indeed, my answer is absolutely wrong :D the last parameter is for encoding type like Base64 or ASCII. Well sorry I can't help you with your issue :/ – Benoit Jul 11 '13 at 07:25
  • It's OK. I had done that mistake in my code as you did before. Aslo, still thank you for your effort. However, please remove the answer back to 0, if it is you who mark that. My reputation is not enough to get it back to 0. – Owen Zhao Jul 11 '13 at 12:18
0

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.

user1712200
  • 329
  • 5
  • 8
0

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()

dev_swat
  • 1,264
  • 1
  • 12
  • 26