Max Asura
Max Asura

Reputation: 821

preference manager

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

Answers (1)

joel
joel

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:

  • Retrieve the latest preferences when you need them. Call your preferences method() just before you are needing the latest preferences. For example, put it in your onUpdate(...) method if you need latest preferences there.
  • Become notified when the preferences are changed. Use a listener to listen for changes of the preferences. The SharedPreferences interface defines the OnSharedPreferenceChangeListener which you can implement and pass to your SharedPreferences.

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

Related Questions