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

守給你的承諾、 提交于 2019-12-04 10:06:25

问题


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


回答1:


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.




回答2:


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.




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



回答4:


Such approach will work

webView.loadDataWithBaseURL("fake-url", "<html></html>", "text/html", "UTF-8", null);
webView.loadData(htmlBuilder.toString(), "text/html", "UTF-8");



回答5:


You need to loadDataWithBaseURL in main thread




回答6:


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



回答7:


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


来源:https://stackoverflow.com/questions/4096783/android-webview-1st-loaddata-works-fine-subsequent-calls-do-not-update-disp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!