Had to load data twice to make WebView refresh in Android

Owen Zhao picture Owen Zhao · Jul 6, 2013 · Viewed 7.6k times · Source

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?

Answer

M. Usman Khan picture M. Usman Khan · Oct 30, 2013

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