Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 683
Owen Zhao Avatar asked Jul 06 '13 10:07

Owen Zhao


2 Answers

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);
like image 190
M. Usman Khan Avatar answered Nov 20 '22 08:11

M. Usman Khan


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.

like image 1
Benoit Avatar answered Nov 20 '22 07:11

Benoit