views:

157

answers:

1

I've got an android app that pulls a random 20 questions (rows) as a cursor from a SQLite DB and loops through all the questions to ask the user all 20 questions.

Is there some way to save the cursor's state/location when the activity is paused or stopped so that when the activity resumes the cursor is restored and in the same position as it was when the activity was paused/stopped?

If you need me to post my code just ask.

Thanks for your time!

A: 

Getting it to save the position it was at is very easy, just put some code in the onPause method like so:

protected void onPause(){
 position = yourCursor.getPosition();
 super.onPause();
}

where position is a class field (i.e. declared outside of any method, usually at the top). Then in onResume you can just move your cursor back to that position. If you aren't familiar with onPause()/onResume(), read up on the activity lifecycle. However, I suspect you want to save which questions were randomly selected as well. I don't think your cursor would persist across the onPause/onResume since they're generally closed in onPause to avoid memory issues. The only thing I can think of, but I'm no expert, is that you'd have to save the rowIDs of the questions and then requery the database to get those rows. How clumsy that solution is depends on how you query for the random questions in the first place. If you already generate 20 random numbers and then feed that into your query, then you can just reuse the same method but with the 20 saved rows instead.


This was too long to fit as a comment:

@Ryan: Indeed, saving the position without the cursor state is pretty useless if you can't recreate an identical cursor - that's what I was saying in the second half of my reply. I assume that your RANDOM() is a standard SQL function? (What's the syntax for that in Android by the way? I couldn't get my database to accept it.) If so, then you might have to do the brute force method to recreate your cursor. By that I mean requery the database with a "rowId = ? OR rowId = ? OR rowId = ? OR...." with the selection args being the rowIds you retrieved from your original cursor. It's not pretty and there's probably a better way to do it, but that will work. Building the SELECT String would be easy with a loop like so:

String selectQuery = "";
String[] selectArgs = new String[savedRows.length];
for (int i = 0; i < savedRows.length; i++){
    selectQuery = selectQuery.concat("rowID = ? OR ");
    selectArgs[i] = Long.toString(savedRows[i]);
}

//Remove the last " OR " you'd have in the string
int index = selectQuery.lastIndexOf(" OR ");
selectQuery = selectQuery.substring(0, index);

This is assuming that you save your rowIds from the original cursor into a long[] savedRows in the onPause method. You can then pass those into a new database query.


Again, too long for comments:

@Ryan: Good point, they might not get pulled back in the same order. You could experiment to see, but it'd be hard to tell whether it was fluke or by design that it was always returned in the same order. Ok, idea number 3 then is creating an intermediate table which is filled with the 20 random questions. This table would have a column for its row number (giving you the order of the questions) and then a foreign key column with the rowId of the question in the questions table. Retrieving your questions in the same order from this table would be easy, and you could drop the table once the user finishes all the questions. Or, you could even keep the table so that the user can see how well they did on past question sets - but that's a whole different feature.

Note: I should have mentioned onSaveInstanceState as well in my first post. onPause is a good place to make quick saves before changing activities, but simply saving the position into an int isn't foolproof. Obviously it resets when the user starts a new quiz (as it should), but it also resets if while the user was looking at another activity, the OS had to kill your process for memory. In that case, when the user returns to the activity it has to restart from onCreate, and thus it loses variable data. This is intended to work with the onSaveInstanceState() method, which supplies you with a Bundle into which you can save your data. This same Bundle is then supplied in onCreate, allowing you to reload whatever is necessary and make it look like the app was never killed.

Steve H
@Steve H: Thanks for the response. I'm actually populating the cursor using the query method of the SQLiteDatabase Class that accepts a limit parameter. I pass in RANDOM() for the order by param and 20 for the limit param. I'm not sure that storing the cursor position will do me much good w/o storing the actual row order of the cursor as well. Hence my need to save the state of the cursor itself.
Ryan
@Steve H: Yes, the RANDOM() works as a standard function in the SQLite Database. Try this query "SELECT * FROM SOME_TABLE ORDER BY RANDOM()". I do see what you're saying but if the rows aren't returned in the exact order in which the original cursor pulls them I don't think that what you're saying would help in this case would it? I pulled 20 random questions then the activity is paused/stopped on question 5. When I resume the activity and pull the rows again would the rows be pulled in the exact same order they were in the originally using your method above?
Ryan
@Steve H: I really like the temp table idea. However I guess I should have mentioned that I'm only concerned with saving the state of the cursor when the orientation of the phone is changed. I found another post on stack overflow that references using onRetainNonConfigurationInstance() and that works like a dream for what I need though I may end up tweaking my app to use your temp table idea in the end. Thanks for the helping hand!
Ryan