3

How am i supposed to reset a WebView state (cache, cookies, and everything else)?

What I want is that the webview state, preferences, etc. is completely reset to the default (when we first ran the app, or after we have cleared the app data from settings).

AshAR
  • 221
  • 1
  • 13
Manza
  • 3,080
  • 3
  • 33
  • 51

2 Answers2

2

Initialize the WebView with the following settings:

Don't use the cache, load from the network.

mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

Sets whether the WebView should save form data.

mWebView.getSettings().setSaveFormData(boolean save) 

Then when you want to wipe out, do:

mWebView.clearCache(true);

This clears the resource cache including the disk files.

Gayan Weerakutti
  • 7,029
  • 50
  • 55
0

I think this question may have been answered:

To clear the cache of your WebView, you can use the following method: https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean)

Usage:

WebView webView;
webView.clearCache(true);

Be aware that the cache is per-application, which means that if you are using this method, you are not only going to clear the cache of the very WebView you are using but also all the other one of your application.

Regarding cookies, the abstract class CookieManager provides a removeAllCookies() for API < 21 or removeAllCookies(ValueCallback < Boolean >) for API >= 21: https://developer.android.com/reference/android/webkit/CookieManager.html

Usage :

CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
Community
  • 1
  • 1
Fred B.
  • 1,331
  • 9
  • 9