android - WebViewPool,从 WebViewPool 重用 WebView 时,最后一个 html 页面显示在新 url 之前
问题描述
有一个WebViewPool,当 Activity/Fragment 被销毁时,webview 将被重置并添加到 WebViewPool。
下面是WebViewPool的代码:
public class WebViewPool {
private static volatile WebViewPool sINSTANCE;
private int mMaxSize;
private List<WebView> mAvailableList;
private List<WebView> mInUsedList;
private IWebViewPoolFactory mFactory;
private WebViewPool() {
}
public static WebViewPool getInstance() {
if (sINSTANCE == null) {
synchronized (WebViewPool.class) {
if (sINSTANCE == null) {
sINSTANCE = new WebViewPool();
}
}
}
return sINSTANCE;
}
public void init(IWebViewPoolFactory factory,boolean lazy) {
init(2, factory,lazy);
}
public void init(int maxSize, IWebViewPoolFactory factory,boolean lazy) {
mMaxSize = maxSize;
mFactory = factory;
mAvailableList = new ArrayList<>(maxSize);
mInUsedList = new ArrayList<>(maxSize);
if (!lazy) {
create();
}
}
private synchronized void create() {
if (mFactory == null) {
return;
}
for (int i = 0; i < mMaxSize; i++) {
WebView webView = mFactory.create(new MutableContextWrapper(APP.getApplicationContext()));
mAvailableList.add(webView);
}
}
/**
* get webview form pool
* @param context
* @return
*/
public synchronized WebView getWebView(Context context) {
if(!(context instanceof Activity)){
throw new IllegalStateException("Context must be Activity");
}
WebView webView = null;
if (mAvailableList.size() > 0) {
webView = mAvailableList.remove(0);
} else {
if (mFactory != null) {
webView = mFactory.create(new MutableContextWrapper(APP.getApplicationContext()));
}
}
if (webView != null) {
((MutableContextWrapper) webView.getContext()).setBaseContext(context);
mInUsedList.add(webView);
}
return webView;
}
/**
* reset/destroy webview when activity/fragemnt is destroyed
* @param webView
*/
public synchronized void restWebView(WebView webView) {
if (webView == null || mFactory == null) {
return;
}
mFactory.reset(webView);
((MutableContextWrapper) webView.getContext()).setBaseContext(APP.getApplicationContext());
if (mInUsedList.contains(webView)) {
mInUsedList.remove(webView);
if (mAvailableList.size() < mMaxSize) {
mAvailableList.add(webView);
} else {
mFactory.destroy(webView);
}
} else {
mFactory.destroy(webView);
}
}
}
以下是一些reset
功能代码:
public void reset(WebView webView) {
if(webView==null){
return;
}
ViewParent viewParent = webView.getParent();
if (viewParent!=null) {
((ViewGroup)viewParent).removeView(webView);
}
webView.stopLoading();
webView.clearCache(false);
webView.loadUrl("about:blank");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
webView.clearHistory();
}
}, 1000);
}
但是当重用webview时,有时会在新的url之前首先显示最后一个html页面。并非每次都发生。我在谷歌搜索,但没有工作。有人知道原因吗?谢谢!
解决方案
这个问题终于解决了!原因是在未加载时将重置的 WebView 添加到可用列表中,about:blank
因此clearHistory()
不起作用。
因此,重置 webview 但在活动/片段被破坏时不添加到可用列表中,clearHistory()
当onPageFinished()
url 为时调用about:blank
:
@Override
public void onPageFinish(String url, boolean success) {
if("about:blank".equals(url)){
webView.clearHistory();
//then add the webview to available list;
}
}
推荐阅读
- java - 如何将信任库和密钥库添加到 PCF(Pivotal Cloud Foundry)中的 springboot 应用程序
- python - Elpy 中有对象浏览器吗?
- powershell - VS Code PowerShell 远程会话寻找本地配置文件
- javascript - Date.prototype.getHours() 行为因地区而异
- git - git 命令的 ZSH 选项卡补全未按预期工作
- java - Spring Kafka Test with Confluent Kafka Avro Serializer 找不到 ZKClientConfig
- java - 如何将文件中的二维浮点数组作为字节写入?
- javascript - 在 div[rectangle] 周围绘制和定位 div[square] 的最佳方法是什么
- android - java.lang.RuntimeException:执行 doInBackground() Kotlin 时发生错误
- java - 数据流的 Beam 模板不会上传到存储桶