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

相关推荐
Yeyu1 分钟前
Android 卡顿诊断 SDK:从痛点出发的设计思考
android
AwakeFantasy24 分钟前
关于Codex中转站生图比例问题的解决记录
数据库·redis·缓存
上天_去_做颗惺星 EVE_BLUE33 分钟前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
我命由我1234543 分钟前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime
剑傲娇2 小时前
【计算机组成原理】 数据通路 之单总线结构
缓存
黄林晴2 小时前
Google Play 全面进化:AI 驱动增长,从上架到收入全链路重构
android·google
夜白宋3 小时前
【Redis深入】一、快的原因
数据库·redis·缓存
qq3621967053 小时前
Android 12/13/14/15 Google Play 兼容性检查指南:设备不兼容怎么办?2026最新解决方案
android·gitee
韩曙亮3 小时前
【错误记录】flutter attach 附加设备 执行报错 ( 附加设备注意事项 )
android·javascript·flutter·flutter attach