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

相关推荐
白菜欣7 小时前
Linux — 进程控制
android·linux·运维
phltxy8 小时前
Redis 事务
数据库·redis·缓存
俩个逗号。。10 小时前
Gradle 踩过的坑
android
1892280486110 小时前
NV265固态MT29F32T08GSLBHL8-24QMES:B
大数据·服务器·人工智能·科技·缓存
土星碎冰机13 小时前
ai自学笔记(3.安卓篇,制作app
android·笔记·ai
bksczm13 小时前
缓存区细则
缓存
接着奏乐接着舞14 小时前
java 数据结构
数据库·redis·缓存
随遇丿而安14 小时前
专题:Glide / Coil / Fresco,不是三种写法,而是三套图片加载思路
android
许长安14 小时前
Redis 跳表实现详解
数据库·c++·经验分享·redis·笔记·缓存
只可远观15 小时前
Android 自动埋点(页面打开 / 关闭 + 点击事件)完整方案
android·kotlin