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

相关推荐
用户41659673693555 分钟前
深度解析 Android 权限机制:从清单注册到 Android 14 适配实战
android
Nerve1 小时前
GalleryPicker:一个基于 Android 官方 Photo Picker API 封装的现代图片/视频选择库
android
乄bluefox2 小时前
如何快速迁移redis - RedisShake(RDB方式)
数据库·redis·缓存
伐尘2 小时前
【MySQL】MySQL 有效利用 profile 分析 SQL 语句的执行过程
android·sql·mysql
少许极端3 小时前
Redis入门指南:从零到分布式缓存-string类型
redis·分布式·缓存
Haha_bj3 小时前
七、Kotlin——扩展(Extensions)
android·kotlin
urkay-3 小时前
Android getDrawingCache 过时废弃
android·java·开发语言·kotlin·iphone·androidx
技术摆渡人3 小时前
Android 14系统深度分析
android·linux·驱动开发
帅得不敢出门4 小时前
Android8 Framework实现Ntp服务器多域名轮询同步时间
android·java·服务器·python·framework·github
走在路上的菜鸟4 小时前
Android学Dart学习笔记第十一节 错误处理
android·笔记·学习·flutter