0

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

Community
  • 1
  • 1
helper
  • 15
  • 5
  • Your questions are answered pretty well by those links you provided...that's probably why noone is adding to this. – M.Bennett Aug 28 '13 at 19:59
  • Yes I understand that- all I really need to know is if there are other ways of accessing SharedPreferences other than through the UI thread.. – helper Aug 29 '13 at 08:56
  • Of course, you can access sharedPreferences from anywhere in your app. This isn't bound to a thread. The worry of running this in the UI thread is that execution should be carefully planned since access can cost quiet a bit of time and make the app lag. – M.Bennett Aug 29 '13 at 09:35

1 Answers1

0
    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