9

Being new in Android world and advancing with joy day by day ;) I would like to share examples about common usage.

Here comes example about using SharedPreferences with generic LocalStore class.

create a common class to be used by your main activity or by any of sub-activity.

    public class LocalStore {

        private static final String TAG = "LocalStore";
        private static final String PREF_FILE_NAME = "userprefs";

        public static void clear(Context context) {
            clear(context, "unknown");
        }
        public static void clear(Context context, String caller) {
            Editor editor = 
                context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();
            Log.d(TAG, "caller:"+caller + "|clear LocalStore");
        }

        public static boolean setCustomBooleanData(String key, boolean value, Context context) {
        Editor editor =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putBoolean(key, value);

        return editor.commit(); 
    }
    public static boolean getCustomBooleanData(String key, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);

        return (savedSession.getBoolean(key, false));
    }

    public static boolean setCustomStringData(String key, String value, Context context) {
        Editor editor =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(key, value);

        return editor.commit(); 
    }
    public static String getCustomStringData(String key, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);

        return (savedSession.getString(key, null));
    }


    public static boolean isCustomStringExistInLocal(String customKey, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);

        return (savedSession.getString(customKey, null))==null?false:true;
    }

public static boolean saveObject(String objKey, Serializable dataObj, Context context) {
        Editor editor =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(objKey, ObjectSerializer.serialize(dataObj) );

        Log.d(TAG, "savedObject| objKey:"+objKey+"/" + dataObj.toString());

        return editor.commit(); 
    }
    public static Object getObject(String objKey, Context context) {
        SharedPreferences savedSession =
            context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);

        Object dataObj = ObjectSerializer.deserialize(savedSession.getString(objKey, null));

        return dataObj;
    }

    }

Note: You can use ObjectSerializer from here

Enjoy!

Additional update: I implemented a library to use MEMDISKCACHE and SHAREDPREF as GENERIC_STORE anyone interested can use it from
-> https://github.com/wareninja/generic-store-for-android

Yilmaz Guleryuz
  • 9,313
  • 3
  • 32
  • 43
  • 13
    StackOverflow FAQ says: " It's also perfectly fine to ask and answer your own question, as long as you pretend you're on Jeopardy: phrase it in the form of a question." – Peter Knego Oct 31 '10 at 22:00
  • Could you please make this a proper question and answer instead of everything being in the question body. Otherwise it may get deleted. – Kev Sep 11 '12 at 22:52
  • 2
    It is nice of you to share :) , but I think this belongs in a blog rather than a question and answer site. – Mohamed_AbdAllah Sep 12 '12 at 09:12
  • you should have posted the ObjectSerializer class too – Adi Aug 26 '13 at 11:20
  • @adi ObjectSerializer is reused from Apache library, link is included in my post. – Yilmaz Guleryuz Aug 27 '13 at 07:08

2 Answers2

2

Assuming you want some tips on how to improve it even more, here you go.

  • Usually Android follows a convention to keep the Context variable as the first parameter. It is good practice to do this when creating any general library.
  • If you want to make it more generic, why not try method overloading? It will give the developer more flexibility when setting values much like how extras are handled in the Intent class.

For example:

public static boolean setData(Context, String key, boolean value) {
        Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putBoolean(key, value);
        return editor.commit(); 
    }
public static boolean setData(Context, String key, String value) {
        Editor editor = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE).edit();
        editor.putString(key, value);
        return editor.commit(); 
    }

So you can simply call the overloaded functions like this:

setData(this, "myBoolean", true);
setData(this, "myString", "Its Awesome");
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
Umair
  • 1,206
  • 1
  • 13
  • 28
0

like this

 fun <T> putData(key: String?, item: T) {
        val editor = localSharedPreferences.edit()
        val list = gson.toJson(item)
        editor.putString(key, list)
        editor.apply()
    }