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

相关推荐
晚霞的不甘1 小时前
Flutter for OpenHarmony从零到一:构建《冰火人》双人合作闯关游戏
android·flutter·游戏·前端框架·全文检索·交互
2601_949833391 小时前
flutter_for_openharmony口腔护理app实战+饮食记录实现
android·javascript·flutter
独自破碎E1 小时前
【滑动窗口+字符计数数组】LCR_014_字符串的排列
android·java·开发语言
stevenzqzq1 小时前
compose 中 align和Arrangement的区别
android·compose
VincentWei952 小时前
Compose:MutableState 和 mutableStateOf
android
jian110582 小时前
Android studio配置flutter,mac Android studio 发现苹果手机设备
android·flutter·android studio
万象.2 小时前
redis持久化:AOF和RDB
数据库·redis·缓存
cheungxiongwei.com2 小时前
深入解析 DNS 缓存与 TTL:工作原理、修改生效机制与优化策略
缓存
2501_940007892 小时前
Flutter for OpenHarmony三国杀攻略App实战 - 性能优化与最佳实践
android·flutter·性能优化
!chen3 小时前
Redis快速实现布隆过滤器
数据库·redis·缓存