I have a quiz app that shuffles everytime it is run. What is the most elegant way of saving a game state locally so that my user can resume the quiz using the same ordered list as before, as well as other primitives such as score, level, life points, etc.
I already have a bundle that saves this state when the game is paused. How can I retain the state when my activity is destroyed.
I know of shared preferences, but there must be something more elegant?
I think we need to understand firstly that there is several possibilities to save data in your app (with possibility to re-enter application and restore data):
1) Save on disc (it fast, but you can erase this data, for example, when reinstall application)
2) Save on remote disc (it slow, but it will keep your game progress throug removing and reinstalling app).
So first of all you need to choose one of this possibilities. I think in your case it will be enough to keep changes locally on disc (besides remote storage cost some additional $$)
In first possibility there are several variants to implement it:
- through shared preferences
- through saving in file (internal storage)
- through saving in file (external storage)
- through keeping data in database
I think last variant is not for you, since you want to just save state of game.
And 'external storage' variant is not for you, too, because game state is private data of your application.
So you need to choose between shared preferences and internal storrage.
In this dilemma, you can think about what you like more. For me shared preferences provides convenient and async way to save data, which is more preferable in game. But you need to think about how many data you want to keep.
If it's not big, choose Shared preferences. And if you do enance your way to save in SharedPreferences (write less code and make it more readable), you can import Gson or Jackson library. Then pick out special GameState class for your game and just convert it to json string and save. Same for restoring. Pick string from prefs and parse it to GameState. That's all.
Hope it helps. :)
I suggest you to use a database and a cursor for retrieving the huge data list. https://developer.android.com/training/basics/data-storage/databases.html
SQLite is very fast and not hard to implement.
Related
Im saving datas from my db/user into a gson formated ArrayList in SharedPreferences. Now my question :
Is it safe to save these datas (or data in general) into Sharedpreferences. Are users able to read these gson Arraylists out ? Maybe from SD card ,in a folder or somewhere else.
Thank you !
They are stored as xml files in your app directory, with permissions that allow only your app to access them. But on rooted device they are easily accessible. If you are concerned with security then you may use encryption, those projects might be usefull to you:
https://github.com/rtoshiro/SecureSharedPreferences
https://github.com/sveinungkb/encrypted-userprefs
still those projects does not give you 100% guarantee, hacker may decompile your apk and find keys used to encrypt shared preferences. So if your data is of use only for short time then remember to remove it from your device once user has finished using it. You may for example keep data on server and download it only when needed, caching locally only for short time - when its needed.
SharedPreferences is just a file located in phone private memory. So user can't access it but root can. Root can everything and many users have root's nowadays. You shouldn't store fragile data there
Android SharedPreference security
You can read all shared preferences Data
The SharedPreferences class provides a general framework that allows
you to save and retrieve persistent key-value pairs of primitive data
types.
To see the information in the store you need to know the important thing from the data. This will make reading through the information super easy. But as simple as it's to keep a tiny bit of data as difficult it's to keep and browse large structured data since you need to define key for every data, in addition you can't really search inside the data except you've got a certain concept for naming the secrets.
Please read Android SharedPreference security
After reading the documention it seems that onSaveInstanceStaate works per instance (as the name suggests). I am just wondering what the preffered method of storing data is so that it is available for all instances of that activity?
As MaciejGorski mentioned in his comment, there are different levels of data storage available in Android:
Shared preferences
Internal storage
External storage
SQLite database
Network
From personal experience, the lower you go down this list, the more complicated your implementation will become. Thus, if you are simply trying to save simple data for your app to be shared among different instances of an activity (or of multiple activities), shared preferences are certainly the way to go. You can even create private shared preferences, which only your app can access.
In any case, check out this SO answer for how to implement them: How to use SharedPreferences in Android to store, fetch and edit values
I am looking to store simple variables so that if the app process is stopped the data will still be available when it relaunches. I looked into extending the Application class and sharedPreferences. To me, it seems that extending Application is good for temporary global variables that are deleted when the app process is stopped. sharedPreferences however saves the variable onto a file so that is is always available. What are the differences between the two, and what are their optimal uses? Also, what is the best for storing variables that you want to keep even if the app is stopped?
When your app's process is killed, any data stored in the Application class will be lost. You should only use that for storing data that is not needed across multiple launches and uses of your app.
For storing simple data like highscores, sound preference, showing a dialog at startup preference, your best bet would be SharedPreferences. For more comprehensive data, like a list of purchases made by an user in your app, or notes in a todo list app, you should use an SQLite Database.
Beyond that, if you want to store files like PDFs etc. or images (images can also be saved into a database), you can use the internal storage or the external storage (may be an SD Card, or a partition on the internal storage). Please keep in mind that on most devices, internal storage is very limited and you shouldn't save excessively large file there.
This part of the documentation should help you with storage options.
I have 3 tabs, each a separate activity. I want to save data when user clicks save on either of the tabs. There are couple of options available; shared preference, global variables or saving the objects in context.
EDIT:I have to save an image & textfield
Android Shared Preferences
Store Objects in ApplicationContext
Any suggestions on which method to pick ?
thanks
That is entirely dependent upon the length of time you wish to store them for.
Storing in the Application Context will not persist data after the application has ended. It just stores in memory.
Shared preferences is a possibility for your string data but image data would require some manipulation and may have other restrictions for example size of data (TBC).
Please consult the relevant SDK documentation on Data Storage
The most convenient way would be to save the text in shared preferences and the image to disk. Refer to the getDir() function if you want to store in the application directory
my application consists of < 10 activities, which cover the wizard part. Each of these activities should collect user data and save the current status, which should be resumed, if the application is unexpectedly terminated.
Now, as the collected data seems to be 'private', the Dev Guide suggests three possibilities to save data:
Shared PreferencesSeems appropriate for my purpose; key/value would be perfectly fine for my kind of data
Internal StorageImo an I/O on every intent is an overkill
SQLite DatabaseFine, too
The thing I'm curious about now, is this the right approach to choose a home for my data? Are there any guidelines or best practices concerning storage on Android devices?
Shared Preferences uses IO - as it just an xml file in data folder of your application. Choose the one which simplier for you to implement. I would use Shared Preferences.
As for "home for data" - it is ok. The storage possibilities are already well-designed for you, so you shouldn't be worry about it. Just use the one which fits your purposes the best.