Android WebView清除缓存

一般在使用完成之后在onDestroy方法中进行处理:

复制代码
@Override
public void onDestroy() {
    super.onDestroy();
    if (webView != null) {
        ViewParent parent = webView.getParent();
        if (parent != null) {
            ((ViewGroup) parent).removeView(webView);
        }
        webView.stopLoading();
        // 退出时调用此方法,移除绑定的服务,否则某些特定系统会报错
        webView.getSettings().setJavaScriptEnabled(false);
        webView.clearView();
        webView.removeAllViews();
        webView.destroy();
        webView = null;
        //清除cookie
        CookieSyncManager.createInstance(mContext);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.removeAllCookie();
    }
    super.onDestroy();
}

很多时候在清理时候,如再次加载与cookie相关联的网页,会读取到cookie缓存,这时需要在应用程序退出或者当前页面退出进行清理:

CookieSyncManager.createInstance(mContext);

CookieManager cookieManager = CookieManager.getInstance();

cookieManager.removeAllCookie();

相关推荐
冬奇Lab20 分钟前
稳定性性能系列之九——启动性能优化:Boot、冷启动与热启动
android·性能优化
STCNXPARM24 分钟前
Android 显示系统 - View体系、WMS
android·wms·view·android显示子系统
weixin_4469388727 分钟前
谷歌play上架广告app
android
jayaccc27 分钟前
前端缓存全解析:提升性能的关键策略
前端·缓存
无限大.28 分钟前
为什么“缓存“能提高系统性能?——从 CPU 缓存到分布式缓存
分布式·缓存
num_killer32 分钟前
小白的RAG缓存
缓存·ai·aigc
一顿操作猛如虎,啥也不是!38 分钟前
redis注册成windows服务,开机启动
数据库·redis·缓存
Kapaseker43 分钟前
初级与中级的Android面试题区别在哪里
android·kotlin
Kapaseker1 小时前
AOSP 发布节奏调整:一年两更
android
huibin1478523691 小时前
开机后无网络,多次重启手机发现开机时间永远是版本编译时间(高通)
android