How can I make the cursor survive a change in orientation?

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) { // Do some stuff here (access DB, etc) } else { // we are returning from configuration change // Feed the cursor to the adapter } 

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.

+8
android rotation configuration cursor
source share
2 answers

You are probably using a controlled cursor. Managed cursors automatically close when Activity is destroyed. You must switch to the uncontrolled Cursor.

+4
source share

Just add this attribute to your activity tag in the manifest file.

Android: configChanges = "orientation | keyboardHidden"

he will allow it

no need to implement anything else

:) It helped me though

0
source share

All Articles