我有一个问题,我使用 loadDataWithBaseURL 方法加载网站。我在 webview 中加载了一个准备好的网站。如果我回到我之前的活动并启动 webview 活动,它会使用 loadDataWithBaseURL() 方法加载相同的准备好的网站,但我会得到一个包含两倍内容的 webview。例如 Hello World 和第二个 Hello World。我的代码如下:
package de.http.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebBackForwardList;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebviewActivity extends Activity {
WebView webview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mywebview);
Bundle bundle= this.getIntent().getExtras();
String param1 = bundle.getString("Weburl");
getDetailWebsite(param1);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setBuiltInZoomControls(true);
loadWebview();
}
public void loadWebview()
{
webview.clearView();
webview.loadDataWithBaseURL("http://branchenportal-schlitz.de", HttpHelperClass.htmlDetailWebsiteCode, "text/html", "UTF-8", HttpHelperClass.htmlDetailWebsiteCode);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
WebBackForwardList mWebBackForwardList = webview.copyBackForwardList();
String historyUrl = mWebBackForwardList.getItemAtIndex(mWebBackForwardList.getCurrentIndex()).getUrl();
if(historyUrl.contains("<html>"))
{
loadWebview();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private void getDetailWebsite(String url)
{
try{
HttpHelperClass.executeHttpGetDetailWebsite(url,this);
}catch (Exception ex)
{
ex.printStackTrace();
}
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
if(url.contains("branchenportal-schlitz.de"))
{
view.clearHistory();
}
super.onPageFinished(view, url);
}
}
@Override
protected void onStop() {
webview.clearCache(true);
webview.clearView();
webview.clearHistory();
webview.destroy();
super.onStop();
}
}
有谁知道我做错了什么?提前致谢。带着敬意