Ogen
Ogen

Reputation: 6709

Android: keeping variables similar to a database

I have an android app that has a database of People. When I close the application, the data base remains and when I turn off the phone and turn it back on, the People are all there on my list.

My app supports other features such as letting the user decide the Person should be displayed. Eg, FirstName then LastName or vice versa etc. When I set these settings then quit the app and start it again. The settings are lost.

How do I keep these variables set? Kind of like they are in the database. What is the easiet/best way?

Thank you.

Upvotes: 0

Views: 39

Answers (2)

xizzhu
xizzhu

Reputation: 905

If your dataset is small, you can use SharedPreferences. Or if your dataset is huge, you should use SQLiteDatabase.

getSharedPreferences("name", Context.MODE_PRIVATE).edit().putString("my_display_toggle", myDisplayToggle).commit(); 
Loading the value: getSharedPreferences("name", Context.MODE_PRIVATE).getString("my_display_toggle", "default_value");

Upvotes: 1

Arash GM
Arash GM

Reputation: 10395

Use SharedPreferences

Interface for accessing and modifying preference data returned by getSharedPreferences(String, int). For any particular set of preferences, there is a single instance of this class that all clients share. Modifications to the preferences must go through an SharedPreferences.Editor object to ensure the preference values remain in a consistent state and control when they are committed to storage. Objects that are returned from the various get methods must be treated as immutable by the application.

How to use SharedPreferences in Android to store, fetch and edit values

Upvotes: 2

Related Questions