PreferenceActivity with multiple preference files

The way to use the preferences file (instead of the default settings) in PreferenceActivity is as follows:

public class MyPreferencesActivity extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); PreferenceManager prefMgr = getPreferenceManager(); prefMgr.setSharedPreferencesName("my_preferences"); prefMgr.setSharedPreferencesMode(MODE_WORLD_READABLE); addPreferencesFromResource(R.xml.preferences); } } 

In this example, we use a Shared Preference named my_preferences. But how will we use more than one user preference in the same PreferenceActivity?

+6
source share
1 answer

Declare the names of your preference files and, returning the preferences, specify the name of this file that you want to access in getSharedPreferences ().

Here I declare two file names: PrefFile and PrefFileNEW; then I pass the corresponding getSharedPreference () names when I get preferences.

  public static final String PREF_FILE_NAME = "PrefFile"; public static final String PREF_FILE_NAME_NEW = "PrefFileNEW"; SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE); { //access your preferences here } SharedPreferences preferences_new = getSharedPreferences(PREF_FILE_NAME_NEW, MODE_PRIVATE); { //access your preferences_new here } 

Hope this helps.

+1
source

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


All Articles