skip to Main Content

Creating a common Shared Preference class in android and using that class to fetch and set data from across all the modules in the project, Please share (.java) files and give a brief on it and also explain the flow like, how to call shared preference common class, how to set data and how to retrieve data. Thanks in advance 🙂

What currently I am following is written below:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, 
MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.apply();

// For fetching data from preference)
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, 
MODE_PRIVATE); 
String name = prefs.getString("name", "No name defined");//"No name 
defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.

”””””””””””””””””””””’

2

Answers


  1. Chosen as BEST ANSWER

    Posting my answer here, this answer helped me, hope this would be helpful for you guys as well Thanks :)

    public class SharedPref {
    
    private static SharedPreferences mSharedPref;
    public static final String PREF_NAME = "NG_CAD_FIRST_RESPONDER";
    public static final String ACCESS_TOKEN = "ACCESS_TOKEN";
    public static final String IS_READ_UNREAD_REQUIRED = "isReadUnreadRequired";
    public static final String Access_State_Device = "accessStateDevice";
    
    
    private SharedPref()
    {
    
    }
    
    public static void init(Context context)
    {
        if(mSharedPref == null)
            mSharedPref = context.getSharedPreferences(context.getPackageName(), Activity.MODE_PRIVATE);
    }
    
     
    public static String getString(String key, String defValue) {
        return mSharedPref.getString(key, defValue);
    }
    
    public static void putString(String key, String value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putString(key, value);
        prefsEditor.apply();
    }
    
    public static Integer getInteger(String key, int defValue) {
        return mSharedPref.getInt(key, defValue);
    }
    
    public static void putInteger(String key, Integer value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putInt(key, value);
        prefsEditor.apply();
    }
    
    
    public static boolean getBoolean(String key, boolean defValue) {
        return mSharedPref.getBoolean(key, defValue);
    }
    
    public static void putBoolean(String key, boolean value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putBoolean(key, value);
        prefsEditor.apply();
    }
    
    public static long getLong(String key, long defValue) {
        return mSharedPref.getLong(key, defValue);
    }
    
    public static void putLong(String key, long value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putLong(key, value);
        prefsEditor.apply();
    }
    
    
    public static float getFloat(String key, float defValue) {
        return mSharedPref.getFloat(key, defValue);
    }
    
    public static void putFloat(String key, float value) {
        SharedPreferences.Editor prefsEditor = mSharedPref.edit();
        prefsEditor.putFloat(key, value);
        prefsEditor.apply();
    }
    
    
    //// Clear Preference ////
    public static void clearPreference(Context context) {
        mSharedPref.edit().clear().apply();
    }
    
    //// Remove ////
    public static void removePreference(String Key){
        mSharedPref.edit().remove(Key).apply();
    }
    
    }
    

    Fetching & Putting data in preference:

    SharedPref.init(getApplicationContext());
    
    
    SharedPref.getString("LATITUDE", ""));
    SharedPref.putString("LATITUDE", latValue);
    

  2. Create a singleton class the initializes one instance from context like so:

    public class MySharedPrefs {
        private static MySharedPrefs sSharedPrefs;
        private SharedPreferences mPref;
        private SharedPreferences.Editor mEditor;
    
        private MySharedPrefs(Context context) {
            mPref = context.getSharedPreferences("NAME", Context.MODE_PRIVATE);
        }
    
    
        public static MySharedPrefs getInstance(Context context) {
            if (sSharedPrefs == null) {
                sSharedPrefs = new MySharedPrefs(context.getApplicationContext());
            }
            return sSharedPrefs;
        }
    }
    

    and you can set values by calling put and get on mEditor, for example:

    public void putString(String key, String val) {
        mEditor = mPref.edit();
        mEditor.putString(key, val);
        mEditor.commit();
    }
    public String getString(String key) {
        return mPref.getString(key, "defaultValue");
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search