title: Android SharedPreferences Best Practices
body: <p>In an application I have been building we rely on SharedPreferences quite a bit, this got me thinking about what is best practice when it comes to accessing SharedPreferences.  For instance many people say the appropriate way to access it is via this call:</p>
  
  <pre><code>PreferenceManager.getDefaultSharedPreferences(Context context)
  </code></pre>
  
  <p>However it seems like this could be dangerous.  If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well.  It seems to me that the better call to use would be:</p>
  
  <pre><code>Context.getSharedPreferences(String name, int mode)
  </code></pre>
  
  <p>This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class.  You could use the fully qualified name of the class to ensure that the file will most likely not be duplicated by someone else.</p>
  
  <p>Also based on this SO question: <a href="https://stackoverflow.com/questions/4371273/should-accessing-sharedpreferences-be-done-off-the-ui-thread">Should accessing SharedPreferences be done off the UI Thread?</a>, it seems that accesses SharedPreferences should be done off the UI thread which makes sense.</p>
  
  <p>Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?</p>