Should I use multiple preference files or just one?

I want to share user configuration data with three separate actions. What is the advantage / disadvantage of one methodology over another (using one or three Prefs files)?

I think that if I use it, I will declare the same constant in the main operation, a la:

public static final String PREFS_NAME = "Pterodactyl"; //Activity 1 

and use getPreferences ();

-OR:

I 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 out of one and a half dozen others" or is it one of these "preferred" over the other, and why?

+4
source share
2 answers

Any preference, global and associated with all actions, will be in the global file. Now, how many files you have to create depends on your taste mainly. Since the properties files simply contain a name / value pair and no grouping matters whether they are in the same file or not.

I recommend one global file for settings, this makes your life very simple. Use below api:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 

There is no file name in this. That way you can live without any preference for the file names in your code.

For a specific activity file, use this when you have properties related to the activity that you want to keep during sessions, for example, you have a scroll position for storage so that when you return even after closing the application, you can restore it.

For reference:

http://developer.android.com/guide/topics/data/data-storage.html#pref

+4
source

I see no reason to use multiple preference files; I use only one for all my actions in my applications.

+1
source

Source: https://habr.com/ru/post/1384619/


All Articles