Having more than one file of common preferences in Android

Is it possible to have more than one SharedPreference file in Android, and if so, how can I configure it?

I plan the first SharedPreference to store about 7 values ​​that will not be used by users. The second SharedPreference will contain custom values.

In this case, if the user exits my application, only the SharedPreference containing the user values ​​will be cleared.

+4
source share
3 answers

Yes, you can support as many general preference files for the application as possible. Just define separate classes for each of them.

+2
source

Yes you can do it.

For example, you can do something as shown below to create an SP

SharedPreferences prefs = getSharedPreferences("countPref", Context.MODE_PRIVATE); 

If you look, then countPref used to uniquely determine sharedPref. Thus, you can take advantage of another general advantage, as shown below, and use it.

 SharedPreferences prefs = getSharedPreferences("countPrefTwo", Context.MODE_PRIVATE); SharedPreferences prefs = getSharedPreferences("countPrefThree", Context.MODE_PRIVATE); 

Good luck

+6
source

You can support multiple SharedPreference files. There is nothing that I came across that prohibits or says otherwise.

You can find the solution here: Android Shared Preferences to help you create a helper class that helps you streamline the storage and retrieval of data from SharedPreference files.

EDIT: Actually, the docs: ( http://developer.android.com/guide/topics/data/data-storage.html#pref ) has getSharedPreferences if you have multiple files.

So the bottom line is yes. You can have multiple SharedPreference files. So there is nothing wrong with this approach.

+1
source

All Articles