9

This error message is pretty clear:

CookieSyncManager::createInstance() needs to be called before CookieSyncManager::getInstance()

But I only get this error because I followed the official documentation:

To use the CookieSyncManager, the host application has to call the following when the application starts:

CookieSyncManager.createInstance(context)

To set up for sync, the host application has to call

CookieSyncManager.getInstance().startSync()

in Activity.onResume()

The error occurs only when the application attempts to resume, not when it starts cleanly.

So, I can probably fix that by moving CookieSyncManager.createInstance(context) to Activity.onResume() but... won't that create a new problem?

(for example, forgetting the previous session cookies every time the app resumes?)

uTubeFan
  • 6,664
  • 12
  • 41
  • 65

1 Answers1

8

I followed the official documentation in one of recent apps and CookieSyncMasnager is working just fine...

I have the following:

onCreate()
    CookieSyncManager.createInstance(this);

onResume()
    CookieSyncManager.getInstance().startSync();

onPause()
    CookieSyncManager.getInstance().stopSync();

I am using the activity context in the createInstance(). You don't mention which context you're using?

You also don't mention doing a CookieSyncManager.getInstance().stopSync() in onPause() (or similar). So perhaps for a resume you are calling CookieSyncManager.getInstance().startSync() twice without an intervening stop?

Torid
  • 4,176
  • 1
  • 28
  • 29
  • @Torid Sorry, I failed to mention that I *am* calling `CookieSyncManager.getInstance().stopSync()` in `onPause()`. The context I am using is, to the best of my current understanding, the main activity's: It is called in initWebView() which is called from the activity's onInit(), in the following manner: `runOnUiThread(new Runnable() { public void run() { initWebView(); } });` Is there anything wrong with this? Thanks and +1. – uTubeFan May 30 '11 at 16:47
  • 1
    Well... runOnUiThread is an Activity method but I don't recognize onInit. What class is that part of? – Torid Jun 02 '11 at 16:15
  • 1
    Plus you don't say how the context is passed down the chain of methods to initWebView - it would be good to check that "this" is passed down correctly from the activity. And you could always get the application context (rather than the activity context) explicitly in the initWebView call using CookieSyncManager.createInstance(getApplicationContext()); – Torid Jun 02 '11 at 16:24
  • @Torid I finally got to figure out that the root cause of the problem is not the context but rather the **order** in which these callbacks are called. `onInit()` is a callback of [TextToSpeech.OnInitListener](http://developer.android.com/reference/android/speech/tts/TextToSpeech.OnInitListener.html) and while it is triggered in `onCreate()`, it is actually called (by the system) *after* `onResume()`. Hence, this error. The question now is: is moving `CookieSyncManager.createInstance(context)` to `Activity.onResume()` the correct solution? – uTubeFan Jun 21 '11 at 19:33
  • @Torid Problem solved by moving `CookieSyncManager::createInstance()` from `onInit()` to `onCreate()` although [no WebView object exists at that point](http://stackoverflow.com/questions/6431242/does-cookiesyncmanager-need-a-valid-instance-of-webview). – uTubeFan Jun 22 '11 at 22:53
  • 1
    Great. It is interesting that you can create a CookieSyncManager instance before the WebView? But hey... if it works. – Torid Jun 23 '11 at 05:45
  • But what about when it's from an `AsyncTask` ? Please look [here](http://stackoverflow.com/questions/23813034/) – Francisco Corrales Morales May 22 '14 at 21:12