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

有帮助吗?

解决方案

    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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top