Trying to establish best practices for SharedPreferences

1.6k Views Asked by At

I am trying to work out the best practice for accessing SharedPreferences. Should they ALWAYS be accessed through the PreferenceManager? And can they ONLY be read and written to via the UI thread?

Any pointers or links to where I can read about this would be greatly appreciated. I have already looked at these links but I do not fully understand the reasoning behind the answers.

Android SharedPreferences Best Practices

Should accessing SharedPreferences be done off the UI Thread?

Thanks

1

There are 1 best solutions below

1
On BEST ANSWER
    public boolean getPrefs()
{


      prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 


      PREF1 = prefs.getBoolean("pref1_key",pref1_default);
      PREF2 = prefs.getString("pref2_key","pref2_default");


 }

use this method to get all preferences you need. Call this in the onResume() or onCreate() method. You needn't worry about UI or anything