T. Rex T. Rex - 1 month ago 29
Android Question

Writing Singleton Class To Manage Android SharedPreferences

i am trying to write a singleton class to oversee all operations involving shared preferences.

I have 3 preference files, general, settings, and temp

I want to be able to use this class to write a preference of a given type, for example:

stg_full_screen: true // as boolean


This is what i have done so far:

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;
}

}


My question is:


  1. Is this a wise thing to do?

  2. How can i efficiently determine the type of a preference?



Thanks

Answer

Usually, I use something like this:

No static Context reference, static getter/setter for each property, when required you can add memory cached value for some property to get it faster from memory instead of reading from SharedPreferences. Clear api.

public class SharedPreferencesManager {

    private static final String APP_SETTINGS = "APP_SETTINGS";


    // properties
    private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE";
    // other properties...


    private SharedPreferencesManager() {}

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(APP_SETTINGS, Context.MODE_PRIVATE);
    }

    public static String getSomeStringValue(Context context) {
        return getSharedPreferences(context).getString(SOME_STRING_VALUE , null);
    }

    public static void setSomeStringValue(Context context, String newValue) {
        final SharedPreferences.Editor editor = getSharedPreferences(context).edit();
        editor.putString(SOME_STRING_VALUE , newValue);
        editor.commit();
    }

    // other getters/setters
}