I'm trying to make the application more convenient, but I have problems with keeping the cursor.
The cursor contains about 13k + lines of data displayed in the ListView , and thus will take quite a while if I make a request every time I change the configuration. In my onRetainNonConfigurationInstance() I return my Cursor and then retrieve it through getLastNonConfigurationInstance() .
However, my recovered cursor seems to be already closed, and thus my adapter can no longer display the list. From what I understand, the cursor has been closed since onDestroy() automatically closes all cursors.
I save the cursor as follows:
@Override public Object onRetainNonConfigurationInstance() { return myCursor; }
And get it like this:
myCursor = (Cursor)getLastNonConfigurationInstance(); if (myCursor == null) {
I insert a stack trace if someone wants to look at it:
01-25 16:57:45.637: ERROR/AndroidRuntime(12976): android.database.StaleDataException: Access closed cursor 01-25 16:57:45.637: ERROR/AndroidRuntime(12976): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217) 01-25 16:57:45.637: ERROR/AndroidRuntime(12976): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 01-25 16:57:45.637: ERROR/AndroidRuntime(12976): at com.test.sample.helper.DictionaryAdapter.bindView(DictionaryAdapter.java:35) [........More ListView-related errors here..........]
I went through the code and I found out that before onRetainNonConfigurationInstance() cursor is still open, but after passing through getLastNonConfigurationInstance() it is already closed.
How can I make the cursor survive a change in orientation? Thanks for the help!
EDIT: Based on Romain's answer, I commented on all of my startManagingCursor() s. I had to tie the dots and think about it! In any case, my application is now experiencing one turn, but its return to its original orientation is still falling. Continuing my debugging and you will find out what I learn.
EDIT2: I think I might have found what causes new errors. I implemented FilterQueryProvider , which returns a new Cursor. What I did was assign the results of this filter to my original cursor. Seems to work so far.
android rotation configuration cursor
Zarah
source share