0

I have a technical question regarding the use of SharedPreferences. I know that this component is supposed to be used to store the user specific preferences of the app (duh). I am currently developing a timer application and need some persistent information about the timer state (i. e. paused, started time, etc, etc.), and that information is not precisely user relevant or related.

I think using SQLite storage for this simple variables is an overload, because I would have a table with only one row, and the cursor management should be asynchronous because querying the information shouldn't be made in the UI thread.

Is there a better data structure to handle those persistent variables, or is it okay if I continue using the preferences?

The information can't be stored in a Bundle because it shouldn't be erased if the application stops running.

Julio Mendoza
  • 310
  • 1
  • 4
  • 11

1 Answers1

1

The comments already answered your question, but I'll provide some details.

Shared preferences are serialized and stored in an XML - for example for the com.android.calendar app it looks like this:

$ adb shell ls -la /data/data/com.android.calendar/shared_prefs/
-rw-rw---- u0_a6    u0_a6         126 2015-08-03 17:21 _has_set_default_values.xml
-rw-rw---- u0_a6    u0_a6         658 2015-08-05 23:05 com.android.calendar_preferences.xml

And the XML is serialized to look like this:

$ adb shell cat /data/data/com.android.calendar/shared_prefs/com.android.calendar_preferences.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="preferences_week_start_day">2</string>
<string name="preferences_alerts_vibrateWhen">never</string>
<int name="preferred_startView" value="3" />
<boolean name="preferences_alerts" value="true" />
<boolean name="preferences_hide_declined" value="true" />
<string name="preferences_alerts_ringtone">content://settings/system/notification_sound</string>
<boolean name="preferences_home_tz_enabled" value="false" />
<string name="preferences_default_reminder">10</string>
<string name="preferences_home_tz">GMT</string>
<int name="preferences_default_cell_height" value="96" />
</map>

You can see how this is done by looking at the source of SharedPreferencesImpl, which calls XmlUtils.writeMapXml to save the preferences...

See line 636 to see how each value in the map is written in the XML.

Also see this question Shared Preferences "limit" where @CommonsWare warns that the entire XML file is read into memory so you wouldn't want to store "100KBS", I assume he means something like "hundreds of KBs". A reasonable arbitrary maximum is probably around a few hundred KBs.

So I'd say as long as the data you're storing is lightweight (i.e. is reasonable to be read/stored) as XML in the format listed above, you're OK and don't need Sqlite.

Community
  • 1
  • 1
G. Lombard
  • 3,569
  • 1
  • 29
  • 30
  • Thanks for the technical information, I will take the information about the limit into account to avoid abusing the preferences. – Julio Mendoza Aug 06 '15 at 14:14