Android WebView - 1st LoadData() works fine, subsequent calls do not update display
Asked Answered
B

7

20

After the 1st call to LoadData() the event onLoadResource fires as it should and the display is fine. Next I want to refresh the screen with a new page, when I use LoadData() the second time the page does not update and onLoadResource() DOES NOT FIRE.

Then second call to LoadData() onlyfires onPageFinished ... onPageStarted never fires!

A work around was to call .reload() after LoadData() but that causes all sorts of problems during the other logic in the activity.

Why doesn't LoadData() work multiple times?

I am using extremely simple HTML, and since using .reload() makes it work my LoadData() statement doesn't seem to be the problem.

Any Ideas would be helpful, TIA

Butterflies answered 4/11, 2010 at 12:47 Comment(0)
A
33

Use

webview.loadDataWithBaseURL("same://ur/l/tat/does/not/work", "data", "text/html", "utf-8", null);

it works fine. loaddata does not refresh next time the data is loaded.

Antibes answered 28/1, 2011 at 21:55 Comment(3)
Anyone got an explanation for why this works? Also calling loadData twice works too (on KitKat anyway) but this solution at least looks as if it's less of a hackDisunion
umm... I must be missing something, but all that came up was a blank page with the word 'data' on itTriquetrous
reason for this.Hailstorm
R
16

For some reason you have to clear the content first. The "load..." methods don't seem to be explicitly appending their content but it doesn't work. I think it used to be WebView.clearView() but that was deprecated. The doc for the deprecated method on the Android site actually tells you to use WebView.loadUrl("about:blank") as a replacement for that method. So...

WebView.loadUrl("about:blank");
WebView.loadData(data, mime, encoding);

...does the trick for me. It seems a little dirty but I wouldn't dare disobey Google! I'm not sure if anyone else is doing this but I am just loading a String in that I had read from an "asset." I'm using it to display help docs. So I'm not using any actual URL's; I'm just using the WebView as an HTML renderer.

Note: For those newbies out there (like me only about a month ago) make sure to replace "WebView" with an instance of your variable. These are not static methods.

Rattler answered 3/3, 2015 at 16:5 Comment(0)
D
3

Such approach will work

webView.loadDataWithBaseURL("fake-url", "<html></html>", "text/html", "UTF-8", null);
webView.loadData(htmlBuilder.toString(), "text/html", "UTF-8");
Duodiode answered 23/10, 2011 at 15:55 Comment(0)
G
3

Those who are still having the issue i found a quick solution just use a handler for this

    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            webView.loadDataWithBaseURL("", html, "text/html", "UTF-8", null);
        }
    }, 10) ;
Gorlicki answered 11/5, 2017 at 13:32 Comment(1)
Calling webView.loadDataWithBaseURL on UI thread did help me. Thanks!Collusive
A
1

You need to loadDataWithBaseURL in main thread

Advertence answered 18/2, 2016 at 5:20 Comment(1)
Add some explanation how this will help OP in fixing issueWikiup
V
-1

I was able to make the browser refresh on each update by giving the html document a different id each time: please see below at // WEBVIEW.

package com.example.scroll;
// philip r brenan at gmail.com, www.appaapps.com 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class MainActivity extends Activity
 {protected void onCreate(Bundle savedInstanceState)
   {super.onCreate(savedInstanceState);
    setContentView(new MyWebView(this)); 
   }
  class MyWebView extends WebView 
   {MyWebView(Context Context)
     {super(Context);
      getSettings().setJavaScriptEnabled(true);
      addJavascriptInterface(this, "Android");   
      new Thread()
       {public void run()
         {for(int j = 0; j < 100; ++j)
           {post(new Runnable()
             {public void run()
               {loadData(content(), "text/html", "utf-8"); // Display in browser
               }
             });    
            try {Thread.sleep(5000);} catch(Exception e) {}
           }  
         }
       }.start();
     } 
    int c = 0, C = 1;
    String content() 
     {final StringBuilder s = new StringBuilder();
      //s.append("<html id="+(C++)+"><body>"); // WEBVIEW REFRESHES CORRECTLY *************** 
      s.append("<html><body>");              // WEBVIEW DOES NOT REFRESH ******************

      s.append("<h1 id=11>1111</h1>");
      s.append("<script>location.href = '#22';</script>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;

      s.append("<h1 id=22>2222</h1>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;
      Log.e("AAAAAA", "content="+s.toString());
      s.append("</body></html>");
      return s.toString();
     }
   } 
 } 
Venery answered 27/8, 2013 at 14:24 Comment(0)
F
-1
String urlUnique = String.format("http://%s", java.util.UUID.randomUUID().toString());
                    webView.loadDataWithBaseURL(urlUnique, "<html></html>", "text/html", "UTF-8", null);
                    Thread.sleep(200);
                    webView.loadData(htmlData, "text/html", "UTF-8");
Fafnir answered 22/9, 2014 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.