Search code examples
androidandroid-asynctaskpersistentonresumeonpause

Semi-persisting data for one screen only


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!


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.