Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Possible to have multiple distinct Shared Preferences per app?

Tags:

android

Is it possible to have multiple Shared Preferences per app? If you create a PreferenceActivity, the values by default are persisted to /data/data/[PACKAGE_NAME]/shared_prefs/[PACKAGE_NAME]_ preferences.xml

Is there a way to have multiple such files and which one to use for a given PreferenceActivity?

like image 793
zer0stimulus Avatar asked Sep 24 '10 02:09

zer0stimulus


People also ask

Can an app have multiple shared pref files?

Yes you can maintain as many shared preference files for an app as you can.

What is the maximum number of shared preferences you can create in Android?

there is no limit in Shared Preference.

Which method do you use if you need multiple preference files?

getSharedPreferences() — Use this if you need multiple shared preference files identified by name, which you specify with the first parameter. You can call this from any Context in your app. getPreferences() — Use this from an Activity if you need to use only one shared preference file for the activity.

How are shared preferences stored?

Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.


1 Answers

Sure:

If you use PreferenceManager.getDefaultSharedPreferences(context); it will create the file you mention.

If you use context.getSharedPreferences("OtherPrefs", Context.MODE_PRIVATE); it will create a file (and SharedPreferences object) in /data/data/[PACKAGE_NAME]/shared_prefs/OtherPrefs.xml.

like image 149
dhaag23 Avatar answered Oct 18 '22 11:10

dhaag23