Reputation: 10287
I want to share user config data from three separate Activities. What is the advantage/disadvantage to one methodology over the other (using a single Prefs file or three)?
What I'm thinking is if I use one, I'll declare the same const in the main Activity, a la:
public static final String PREFS_NAME = "Pterodactyl"; //Activity 1
and use getPreferences();
-OR:
I'll declare a different const in each Activity, a la:
public static final String PREFS_NAME = "Pterodactyl"; //Activity 1
public static final String PREFS_NAME = "duckbilledPlatypus"; //Activity 2
public static final String PREFS_NAME = "yellowbelliedSapsucker"; //Activity 3
and use getSharedPreferences();
Is it "6 of one and half a dozen of the other" or is one of these "preferable" over the other, and why?
Upvotes: 4
Views: 968
Reputation: 9590
Any Preference which is global and related to all activities would be in global file. Now how many files you should create depends on your taste mainly. As property files just contain name/value pair and no grouping so does not matter whether they are in one file or not.
I recommend single global file for settings, it makes your life very easy. Use below api:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
There is no file name involved it this. So you can live without having any preferences file names in your code.
Regarding Activity specific file, use this when you have properties regarding Activity which you want to retain across sessions like you have scroll position to store so that when you back even after closing app you can restore it.
For reference:
http://developer.android.com/guide/topics/data/data-storage.html#pref
Upvotes: 4
Reputation: 4387
I don't see a reason to use multiple preferences files; I use just one for all of my activities in my applications.
Upvotes: 1