Had to load data twice to make WebView refresh in Android
Asked Answered
S

4

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?

Shushubert answered 6/7, 2013 at 10:7 Comment(4)
I'm having exactly the same issue. Have you found a way to fix it?Gaskell
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.Shushubert
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.Gaskell
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);Shushubert
C
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);
Coelho answered 30/10, 2013 at 12:19 Comment(3)
It is working for me to and actually saved my day. Thanks. Could you explain what is the problem with the first one?Babby
don't know. Looks like a defectCoelho
not work for me. I use css inside asset wv.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html" , "utf-8", null);Shrivel
A
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.

Anthropometry answered 10/7, 2013 at 16:37 Comment(3)
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.Shushubert
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 :/Anthropometry
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.Shushubert
J
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.

Jural answered 11/8, 2018 at 13:57 Comment(0)
R
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()

Remunerative answered 28/3, 2019 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.