Question

what Iam doing is a simple game which when beaten, goes to a new activity, there it starts a AsyncTask and submits user score to the server, server responds with a array of scores (few better than me, me, few worse than me).

What I want to do is persist this array, but only for this screen, then user exists that particular screen data will be forgotten. The game is always landscape so I dont need to worry about screen rotation, but user can jump out of the app and when he comes back and application is not killed it should resume to that screen, as it does, but data is gone (and async task starts off again), so I figured I need to persist that data somehow but writing it to database seemed too much as I would need to worry about deleting it first blah blah.

So I was wondering, what kind of technique would you use to persist data in this situation? As a member variable in Application object? Temporary table? Saved instance (even though putting arraylist in savedInstanceState will be a pain)

Thanks!

Was it helpful?

Solution

I suggest using onSaveInstanceState. This is designed for such cases.

In fact it is not a pain. Use putParcelableArrayList and later getParcelableArrayList in onCreate.

If your data class is as simple as holding String (name) and int (score) making is Parcelable is a good exercise if you haven't implemented it before.

OTHER TIPS

At some points your question is not clear.

What I want to do is persist this array, but only for this screen, then user exists that particular screen data will be forgotten.

By this line it seems you only persists the data only when the activiy is running ( in foreground or background). So in that case there is no need to store the data elsewhere rather than the class variables. So if this is what you want then may be you are calling the asynctask in onResume. SO in that case call the asynctask in onCreate.

But if you want to persists the data even after the application is closed then you can use SharedPreferences to store the array.

It will be more easier to know your problem if you can post your codes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top