Reputation: 821
By preferences.xml an users choose a value from 4 by listpref. After first time that android runs preference(), when an users choose again another value by listfref, how execute preference()? Where I put preference() within my code? thanks!
In the MainActivity, I've
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
preference();
[...] my code: buttons, textview, etc [...]
private void preferences() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
CheckboxPreference = prefs.getBoolean("checkboxPref", true);
ListPreference = prefs.getString("listpref", "");
Upvotes: 0
Views: 1858
Reputation: 480
I suppose you want to know, how you can get the latest preferences or how you can be notified if the user changes a preference. Your preferences() method queries the latest preferences. With calling preferences in the onCreate(...) method you retrieve the latest preferences when the corresponding activity is created. After intially retrieving your preferences I see two possibilities to get the newest preferences:
I have to admit that the first possibility is a rather general answer. The android developers storage options site has more information about using shared preferences.
Upvotes: 1