android - Saving cursor state with activity instance? -
i've got android app pulls random 20 questions (rows) cursor sqlite db , loops through questions ask user 20 questions.
is there way save cursor's state/location when activity paused or stopped when activity resumes cursor restored , in same position when activity paused/stopped?
if need me post code ask.
thanks time!
getting save position @ easy, put code in onpause method so:
protected void onpause(){ position = yourcursor.getposition(); super.onpause(); }
where position class field (i.e. declared outside of method, @ top). in onresume can move cursor position. if aren't familiar onpause()/onresume(), read on activity lifecycle. however, suspect want save questions randomly selected well. don't think cursor persist across onpause/onresume since they're closed in onpause avoid memory issues. thing can think of, i'm no expert, you'd have save rowids of questions , requery database rows. how clumsy solution depends on how query random questions in first place. if generate 20 random numbers , feed query, can reuse same method 20 saved rows instead.
this long fit comment:
@ryan: indeed, saving position without cursor state pretty useless if can't recreate identical cursor - that's saying in second half of reply. assume random() standard sql function? (what's syntax in android way? couldn't database accept it.) if so, might have brute force method recreate cursor. mean requery database "rowid = ? or rowid = ? or rowid = ? or...." selection args being rowids retrieved original cursor. it's not pretty , there's better way it, work. building select string easy loop so:
string selectquery = ""; string[] selectargs = new string[savedrows.length]; (int = 0; < savedrows.length; i++){ selectquery = selectquery.concat("rowid = ? or "); selectargs[i] = long.tostring(savedrows[i]); } //remove last " or " you'd have in string int index = selectquery.lastindexof(" or "); selectquery = selectquery.substring(0, index);
this assuming save rowids original cursor long[] savedrows
in onpause method. can pass new database query.
again, long comments:
@ryan: point, might not pulled in same order. experiment see, it'd hard tell whether fluke or design returned in same order. ok, idea number 3 creating intermediate table filled 20 random questions. table have column row number (giving order of questions) , foreign key column rowid of question in questions table. retrieving questions in same order table easy, , drop table once user finishes questions. or, keep table user can see how did on past question sets - that's whole different feature.
note: should have mentioned onsaveinstancestate in first post. onpause place make quick saves before changing activities, saving position int isn't foolproof. resets when user starts new quiz (as should), resets if while user looking @ activity, os had kill process memory. in case, when user returns activity has restart oncreate, , loses variable data. intended work onsaveinstancestate() method, supplies bundle can save data. same bundle supplied in oncreate, allowing reload whatever necessary , make app never killed.
Comments
Post a Comment