title: Writing Singleton Class To Manage Android SharedPreferences
body: <p>i am trying to write a singleton class to oversee all operations involving shared preferences. </p>
  
  <p>I have 3 preference files, general, settings, and temp</p>
  
  <p>I want to be able to use this class to write a preference of a given type, for example:</p>
  
  <pre><code>stg_full_screen: true // as boolean
  </code></pre>
  
  <p>This is what i have done so far:</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  
  public class SharedPrefManager extends Activity {
  
      // Globals
      private int GENERAL             = 1000;
      private int SETTINGS            = 2000;
      private int TEMP_STORE          = 3000;
  
      private String PREF_GENERAL     = "com.example.general";
      private String PREF_SETTINGS    = "com.example.settings";
      private String PREF_TEMP_STORE  = "com.example.temp_store";
  
  
      private SharedPreferences general;
      private SharedPreferences settings;
      private SharedPreferences tempStore;
  
      private SharedPreferences.Editor general_editor;
      private SharedPreferences.Editor settings_editor;
      private SharedPreferences.Editor temp_store_editor;
  
  
  
  
  
      // Instantiate singleton object
      private static SharedPrefManager ourInstance = new SharedPrefManager();
  
  
      public static SharedPrefManager getInstance() { return ourInstance; }
  
      private SharedPrefManager() {
          // Get handle on all preference files
          general   = getSharedPreferences(PREF_GENERAL, Context.MODE_PRIVATE);
          settings  = getSharedPreferences(PREF_SETTINGS, Context.MODE_PRIVATE);
          tempStore = getSharedPreferences(PREF_TEMP_STORE, Context.MODE_PRIVATE);
  
          // provision editors for all preference files
          general_editor    = general.edit();
          settings_editor   = settings.edit();
          temp_store_editor = tempStore.edit();
      }
  
  
  
      private String read_prefs (String pref_name) {
          // this method reads a preference and returns it
          // ideally, i would want to be able to return appropriate types by request
          // e.g boolean, string
          return null;
      }
  
      private void write_prefs (String pref_name, String pref_val) {
          // this method would take a preference and write the appropriate type to prefs
      }
  
  
      // this method determines where to put a preference by checking the name of the key
      // this works because i use the following naming conventions
      // stg_name for settings, tmp_name for all that goes into tempStore
  
      private String resolve_pref_category (String path) {
          if (path.startsWith("stn"))         return PREF_SETTINGS;
          else if (path.startsWith("tmp"))    return PREF_TEMP_STORE;
          else                                return PREF_GENERAL;
      }
  
  }
  </code></pre>
  
  <p>My question is:</p>
  
  <ol>
  <li>Is this a wise thing to do?</li>
  <li>How can i efficiently determine the type of a preference?</li>
  </ol>
  
  <p>Thanks</p>