flush WebView by clearing Cookies and data - Android android.webkit

Android examples for android.webkit:WebView

Description

flush WebView by clearing Cookies and data

Demo Code

import android.content.Context;
import android.os.Build;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebViewDatabase;

public class Main {

  public static void flushWebView(Context context) {
    clearCookies(context);/*from ww w.j  a  v  a  2 s  .  co  m*/
    clearData(context);
  }

  public static void clearCookies(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
      CookieSyncManager.createInstance(context);
      CookieSyncManager.getInstance().startSync();
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      CookieManager.getInstance().removeAllCookies(null);
    } else {
      CookieManager.getInstance().removeAllCookie();
    }
  }

  public static void clearData(Context context) {
    WebViewDatabase.getInstance(context).clearFormData();
    WebViewDatabase.getInstance(context).clearUsernamePassword();
    WebViewDatabase.getInstance(context).clearHttpAuthUsernamePassword();
  }

}

Related Tutorials