0

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.

Ogen
  • 6,499
  • 7
  • 58
  • 124

2 Answers2

2

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

Community
  • 1
  • 1
Arash GM
  • 10,316
  • 6
  • 58
  • 76
1

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");
wyoskibum
  • 1,869
  • 2
  • 23
  • 43
xizzhu
  • 895
  • 6
  • 9
  • Can you give me an exmaple of using shared preferences for storing and getting a public static variable called myDisplayToggle? – Ogen Oct 09 '13 at 12:16
  • 1
    Saving the value: `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");` – xizzhu Oct 09 '13 at 12:30