0

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!

urSus
  • 12,492
  • 12
  • 69
  • 89

2 Answers2

1

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.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Yep thats what I want to use but somehow savedInstance is null, I guess I have to look elsewere for that problem – urSus May 12 '13 at 02:41
  • @VlastoBennyLava in `onCreate(Bundler savedInstance)` `savedInstance` will be null the first time. After you rotate the phone or process is recreated it will contain data previously added in `onSaveInstanceState`. – MaciejGórski May 12 '13 at 09:03
0

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.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Well, afaik if activity goes to background, it can get destroyed so class variable is no good. I think I will use savedInstanceState but for some reason it null for me now. Thanks a lot for your input. – urSus May 12 '13 at 02:43