기본 콘텐츠로 건너뛰기

Android, SharedPreferences, store Object, sample

//Test : obj( ArrayList<Map>)


public static void saveObj(Context context, Object obj){
        SharedPreferences.Editor editor = context.getSharedPreferences(STORAGE_KEY, 0).edit();
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        editor.putString(OBJ_KEY, json);
        editor.commit();
}
public static Object loadObj(Context context){
        SharedPreferences prefs = context.getSharedPreferences(STORAGE_KEY, 0);
        String json = prefs.getString(OBJ_KEY, null);

        Gson gson = new Gson();
        Object obj = gson.fromJson(json, Object.class);

        return obj;

}

댓글