Alex Rivera | Logout

How can I make my Cursor survive an orientation change?

Asked 2011-01-25T09:23:47.870
9

I am trying to make my app rotation friendly, but I am having some problems saving the cursor.

The cursor holds about 13k+ rows of data displayed in a ListView, and thus would take quite a while if I would do a requery every time the configuration changes. In my onRetainNonConfigurationInstance(), I am returning my Cursor and then retrieving it through getLastNonConfigurationInstance().

However, my retrieved cursor seems to be closed already, and thus my adapter cannot render the list anymore. From what I understand, the cursor was closed since onDestroy() automatically closes all cursors.

I save the Cursor like this:

@Override
public Object onRetainNonConfigurationInstance() {
    return myCursor;
}

And retrieve 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 am pasting the 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 stepped through the code and I found out that as far as onRetainNonConfigurationInstance(), the cursor is still open, but after getting it through getLastNonConfigurationInstance() it i

Edit
Report

1 Answer

0

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

android:configChanges="orientation|keyboardHidden"

it will resolve this

no need to implement anything else

:) It helped me though

answered 2011-01-25T09:33:51.510

Your Answer