Separate preferences for each view in the Android application

I have several views that come and go with the launch of the application. I want each view to have its own personal preferences, which are stored as a presentation ID tag. Above are the "General Settings", which reference auxiliary prefs to get their default values ​​when creating a view.

I have now established that the general settings are standard SharedPreferences. But I have no idea how to create new settings and customize the activity user interface so that the user can change them. Does this look like setting up SharedPreferences?

+4
source share
3 answers

this is not quite what you are asking for, but here is what i am doing:

in my main action, when I call a preference activity, I pass it the name of the user preference file as additional data in the intent:

static final String EXTRA_PREFERENCES_NAME = "android.intent.extra.PREFERENCES_NAME"; ... Intent intent = new Intent(this, Preferences.class); intent.putExtra(EXTRA_PREFERENCES_NAME, preferencesName); startActivity(intent); 

then in my preferences setting, I get the name of the user settings and set it like this:

 public class Preferences extends PreferenceActivity { private String preferencesName = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // get the custom preferences name from the extra data in the intent preferencesName = getIntent().getExtras().getString(MainActivity.EXTRA_PREFERENCES_NAME); // set the preferences file name getPreferenceManager().setSharedPreferencesName(preferencesName); // get the default preferences from XML addPreferencesFromResource(R.xml.preferences); } 

Finally, in my main action, I get specific settings like this:

 SharedPreferences preferences = getSharedPreferences(preferencesName, MODE_PRIVATE); String somePreference = preferences.getString("somePreference", defaultValue); 
+5
source

Somehow I'm not worthy of commenting, but writing an answer, so we go: I would really like to learn how to use sharedPreferences with PreferencesActivity instead of DefaultSharedPreferences.

One way I can think of to achieve this is to let preferenceActivity save the default values ​​of SharedPreferences, and then read these values ​​and save them in sharedPreferences associated with the name that will correspond to the stored values.

But that seems very wrong. So how do you guys do this? Or do you save all your values ​​from any PreferencesActivties settings to defaultSharedPreferences?

0
source

You can use the PreferenceManager to achieve the goal.

-2
source

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


All Articles