i'm loading results database using loadermanager. unfortunately, following code produces staledataexception after rotating device:
@override public void onloadfinished(loader<cursor> loader, cursor cursor) { // if we've returned results, pass them , websearches cursor along displayed if(cursor.movetofirst()) { // cursor containing additional web searches , merge @ end of results cursor matrixcursor searchescursor = getwebsearchescursor(searchterm, false); cursor[] cursors = { cursor, searchescursor }; // todo: figure out why merging cursors here causes staledataexception after rotation cursor results = new mergecursor(cursors); // display cursor in listview adapter.changecursor(results); } // if no results returned, return suggestions web searches else { // cursor containing additional web searches matrixcursor noresults = getwebsearchescursor(searchterm, true); adapter.changecursor(noresults); } // show listview , hide progress spinner togglelistview(show); }
the call getwebsearchescursor() returns matrixcursor additional search prompts accompany returned results. discovered changing adapter.changecursor(results) adapter.changecursor(cursor) fixes error, looks merging matrixcursor returned cursor produces error.
my question is, why?
if results returned, i'd able add additional items returned cursor user has option perform search on couple of websites. there better way merge cursors don't exception after rotation?
this issue came again couple of days ago , fortunate enough stumble upon solution.
i found out should have been using swapcursor() instead of changecursor(). according android docs:
swap in new cursor, returning old cursor. unlike changecursor(cursor), returned old cursor not closed.
...
if given new cursor same instance set cursor, null returned.
that last part seemed key. error mentioned in question above traced cursoradapter choking on merged cursor because closed when tried redraw fragment after rotation. using swapcursor() instead, cursoradapter able reuse "old" merged cursor instead of questioning validity , throwing staledataexception.
i'm making suppositions here; perhaps more knowledgeable in inner-workings of android confirm or deny reasoning.
Comments
Post a Comment