android - How can I make my Cursor survive an orientation change? -
i trying make app rotation friendly, having problems saving cursor.
the cursor holds 13k+ rows of data displayed in listview
, , take quite while if requery every time configuration changes. in onretainnonconfigurationinstance()
, returning cursor , retrieving through getlastnonconfigurationinstance()
.
however, retrieved cursor seems closed already, , adapter cannot render list anymore. understand, cursor closed since ondestroy()
automatically closes cursors.
i save cursor this:
@override public object onretainnonconfigurationinstance() { return mycursor; }
and retrieve this:
mycursor = (cursor)getlastnonconfigurationinstance(); if (mycursor == null) { // stuff here (access db, etc) } else { // returning configuration change // feed cursor adapter }
i pasting stack trace if wants @ 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): @ android.database.abstractwindowedcursor.checkposition(abstractwindowedcursor.java:217) 01-25 16:57:45.637: error/androidruntime(12976): @ android.database.abstractwindowedcursor.getstring(abstractwindowedcursor.java:41) 01-25 16:57:45.637: error/androidruntime(12976): @ com.test.sample.helper.dictionaryadapter.bindview(dictionaryadapter.java:35) [........more listview-related errors here..........]
i stepped through code , found out far onretainnonconfigurationinstance()
, cursor still open, after getting through getlastnonconfigurationinstance()
closed.
how can make cursor survive orientation change? thank help!
edit: based on romain's answer, commented out startmanagingcursor()
s. should have connected dots , thought it! anyway, app survives 1 rotation, flipping original orientation still crashes it. continuing debugging , let know find out.
edit2: think may have found causing new errors. have implemented filterqueryprovider
returns new cursor. did assign results of filter original cursor. seems work far.
you using managed cursor. managed cursors automatically closed when activity destroyed. should switch unmanaged cursor.
Comments
Post a Comment