androidwebviewloaddata

Had to load data twice to make WebView refresh in Android


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?


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