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

相关推荐
用户693717500138420 分钟前
14.Kotlin 类:类的形态(一):抽象类 (Abstract Class)
android·后端·kotlin
火柴就是我29 分钟前
NekoBoxForAndroid 编译libcore.aar
android
Kaede62 小时前
MySQL中如何使用命令行修改root密码
android·mysql·adb
明君879973 小时前
Flutter 图纸标注功能的实现:踩坑与架构设计
android·ios
成都大菠萝3 小时前
Android Auto开发(3)-Audio Integration
android
成都大菠萝3 小时前
Android Auto开发(5)-Audio Integration
android
泡沫·4 小时前
7.LAMPLNMP 最佳实践
android
码码宁4 小时前
六个故事搞懂Fragment 故事1-初识Fragment - NewsHub的模块化革命
android
成都大菠萝4 小时前
Android Auto开发(0)-引言
android
q***33375 小时前
SpringMVC新版本踩坑[已解决]
android·前端·后端