NullPointerException
NullPointerException

Reputation: 37635

How to persist webview cookies between app executions?

It is possible to achieve this currently in Android? I only can find deprecated questions about old methods (CookieSynchManager) which not seems to work for this actually.

It is possible to achieve it? I can't find anything also on the Android Developers' Guide.

Upvotes: 9

Views: 8234

Answers (2)

Edoardo Vignati
Edoardo Vignati

Reputation: 553

Having the same problem...I solved reading the doc here:

https://developer.android.com/reference/android/webkit/CookieSyncManager

For future readers: to force the Cookie sync process you can manually call the flush() method of CookieManager

I personally put in webview the following code:

public class MyWebViewClient extends WebViewClient {

    public void onPageFinished(WebView view, String url) {
      CookieManager.getInstance().setAcceptCookie(true);
      CookieManager.getInstance().acceptCookie();
      CookieManager.getInstance().flush();
    }
}

Upvotes: 12

Manoj Perumarath
Manoj Perumarath

Reputation: 10214

Since CookieSynchManager is deprecated, CookieManager.getInstance() is the CookieManager instance for your entire application. Hence, you enable it by

CookieManager.getInstance().setAcceptCookie(true)

setAcceptCookie(boolean accept);

Sets whether the application's WebView instances should send and accept cookies.

https://developer.android.com/reference/android/webkit/CookieManager.html

Upvotes: 0

Related Questions