1

I need persistent storage for my application for a certain activity. I was going to initially use a database table with 5 columns for holding the data I needed, but then I thought that maybe using sharedPreferences may be lighter weight in this case. So my question essentially is, if I am going to use a database for a single entry, should I just use sharedPreferences because I know databases are known for being a bit heavy.

EGHDK
  • 17,818
  • 45
  • 129
  • 204

2 Answers2

2

If you just want to store the 5 values, then using SharedPreference might be preferable. It is also faster than using database. In addition to allow persisting key-value pairs, SharedPreference also supports saving sets and ArrayList. See Save ArrayList to SharedPreferences. So storing an array in the ShraredPrefernce will suffice.

If you have more structured data, then using database might be better to manage. To know more about when SharedPreference would be good to use and when SQL Lite database, Pros and Cons of SQLite and Shared Preferences question is a good read. Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
1

Yes, sharedPreferences works great for what you are trying to do. It's lightweight and needs the least amount of code to meet your requirement.

Dannie
  • 2,430
  • 14
  • 16