Cursor problem/Stale Data Exception in unusual situation

1k Views Asked by At

Hoping someone can assist with this what seems to me a peculiar problem.. My mind is pulsing a little here as it's blown my understanding of the Android Activity Lifecycle.. Let me try to make things clearer.

Problem: I'm receiving a 'StaleDataException' during the 'getView' method of a custom adapter (extending BaseAdapter) that I use on an activity to populate a GridView. Sounds straight forward so far.. When I first go into the activity, the adapter is working as expected and the grid is populated. I have a button on the activity which fires off an Intent to allow the user to take a photo, and comes back via 'onActivityResult' saving the image to a database. This all worked also. In fact I wrote that portion first, then added the adapter & gridview afterwards.

Now the StaleDataException is occuring when I've gone to the camera to take a pic, and then click OK to return... I have lots of Log statements in most events on my activity, and very bizarrely the StaleDataException is occuring before ANY of these events are triggered... before OnActivityResult/OnResume etc.

SO I'm quite confused as to why the Adapter is being accessed before I've properly returned to my activity and before onActivityResult/OnResume have been called..

Debugging the steps that lead up to the exception, it all occurs on this line :

String contentType = dataCursor.getString(dataCursor.getColumnIndexOrThrow(DbStatics.ACCIDENTS_MEDIA_KEY_TYPE));

What's also baffling me is the dataCursor is actually Open and the first call (getColumnIndexOrThrow) is also returning a value.. so its the getString() which leads to the exception.

So I hope that's clear enough... and really really hope someone might be able to shed some light on what's going on..

Many Thanks,

1

There are 1 best solutions below

0
On

I recently encountered exactly the same problem in almost exactly the same scenario. (I'm using a Gallery rather than a GridView)

In my case, my code worked fine for months - until I recently updated my N1 to 2.3. My solution (after much brain pulsing of my own) was to not call startManagingCursor on the cursor I pass in to my adapter. Instead, I manage the cursor myself.

That seemed to clear the problem up for me....though I couldn't tell you why...maybe someone more experienced can shed some light.

Hope this helps you.