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

相关推荐
雨白6 小时前
掌握 NestedScrolling 嵌套滑动:手写仿知乎折叠主页
android
爬也要爬着前进6 小时前
redis主从搭建
数据库·redis·缓存
Xzaveir7 小时前
别把所有“认证”都塞进 AuthService:实名、一键登录与号码身份的领域拆分
android·人工智能
BerrySen1787 小时前
KMP全栈开发:从Android到AI Agent的技术演进与实践
android·人工智能
AFinalStone9 小时前
Android 7系统休眠唤醒(一)电源管理架构全景图
android·powermanager·电源管理
YM52e9 小时前
鸿蒙Flutter Center居中组件:Align对齐详解
android·学习·flutter·华为·harmonyos·鸿蒙
时间的拾荒人10 小时前
MySQL 视图详解
android·数据库·mysql
祉猷并茂,雯华若锦11 小时前
Appium 3.x安卓按键与通知栏操作全指南
android·appium
码农coding11 小时前
android 12 SurfaceFlinger开机启动分析
android
hunterandroid11 小时前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端