I need to get user objects in many places, which contain many fields. After login, I want to save/store these user objects. How can we implement this kind of scenario?
<there are two file solved your all problem about sharedpreferences
1)AppPersistence.java
public class AppPersistence {
public enum keys {
USER_NAME, USER_ID, USER_NUMBER, USER_EMAIL, USER_ADDRESS, CITY, USER_IMAGE,
DOB, MRG_Anniversary, COMPANY, USER_TYPE, support_phone
}
private static AppPersistence mAppPersistance;
private SharedPreferences sharedPreferences;
public static AppPersistence start(Context context) {
if (mAppPersistance == null) {
mAppPersistance = new AppPersistence(context);
}
return mAppPersistance;
}
private AppPersistence(Context context) {
sharedPreferences = context.getSharedPreferences(context.getString(R.string.prefrence_file_name),
Context.MODE_PRIVATE);
}
public Object get(Enum key) {
Map<String, ?> all = sharedPreferences.getAll();
return all.get(key.toString());
}
void save(Enum key, Object val) {
SharedPreferences.Editor editor = sharedPreferences.edit();
if (val instanceof Integer) {
editor.putInt(key.toString(), (Integer) val);
} else if (val instanceof String) {
editor.putString(key.toString(), String.valueOf(val));
} else if (val instanceof Float) {
editor.putFloat(key.toString(), (Float) val);
} else if (val instanceof Long) {
editor.putLong(key.toString(), (Long) val);
} else if (val instanceof Boolean) {
editor.putBoolean(key.toString(), (Boolean) val);
}
editor.apply();
}
void remove(Enum key) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key.toString());
editor.apply();
}
public void removeAll() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
2)AppPreference.java
public static void setPreference(Context context, Enum Name, String Value) {
AppPersistence.start(context).save(Name, Value);
}
public static String getPreference(Context context, Enum Name) {
return (String) AppPersistence.start(context).get(Name);
}
public static void removePreference(Context context, Enum Name) {
AppPersistence.start(context).remove(Name);
}
}
now you can save,remove or get like,
-save
AppPreference.setPreference(context, AppPersistence.keys.USER_ID, userID);
-remove
AppPreference.removePreference(context, AppPersistence.keys.USER_ID);
-get
AppPreference.getPreference(context, AppPersistence.keys.USER_ID);
An other way to save and restore an object from android sharedpreferences without using the Json format
private static ExampleObject getObject(Context c,String db_name){
SharedPreferences sharedPreferences = c.getSharedPreferences(db_name, Context.MODE_PRIVATE);
ExampleObject o = new ExampleObject();
Field[] fields = o.getClass().getFields();
try {
for (Field field : fields) {
Class<?> type = field.getType();
try {
final String name = field.getName();
if (type == Character.TYPE || type.equals(String.class)) {
field.set(o,sharedPreferences.getString(name, ""));
} else if (type.equals(int.class) || type.equals(Short.class))
field.setInt(o,sharedPreferences.getInt(name, 0));
else if (type.equals(double.class))
field.setDouble(o,sharedPreferences.getFloat(name, 0));
else if (type.equals(float.class))
field.setFloat(o,sharedPreferences.getFloat(name, 0));
else if (type.equals(long.class))
field.setLong(o,sharedPreferences.getLong(name, 0));
else if (type.equals(Boolean.class))
field.setBoolean(o,sharedPreferences.getBoolean(name, false));
else if (type.equals(UUID.class))
field.set(
o,
UUID.fromString(
sharedPreferences.getString(
name,
UUID.nameUUIDFromBytes("".getBytes()).toString()
)
)
);
} catch (IllegalAccessException e) {
Log.e(StaticConfig.app_name, "IllegalAccessException", e);
} catch (IllegalArgumentException e) {
Log.e(StaticConfig.app_name, "IllegalArgumentException", e);
}
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
return o;
}
private static void setObject(Context context, Object o, String db_name) {
Field[] fields = o.getClass().getFields();
SharedPreferences sp = context.getSharedPreferences(db_name, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
for (Field field : fields) {
Class<?> type = field.getType();
try {
final String name = field.getName();
if (type == Character.TYPE || type.equals(String.class)) {
Object value = field.get(o);
if (value != null)
editor.putString(name, value.toString());
} else if (type.equals(int.class) || type.equals(Short.class))
editor.putInt(name, field.getInt(o));
else if (type.equals(double.class))
editor.putFloat(name, (float) field.getDouble(o));
else if (type.equals(float.class))
editor.putFloat(name, field.getFloat(o));
else if (type.equals(long.class))
editor.putLong(name, field.getLong(o));
else if (type.equals(Boolean.class))
editor.putBoolean(name, field.getBoolean(o));
else if (type.equals(UUID.class))
editor.putString(name, field.get(o).toString());
} catch (IllegalAccessException e) {
Log.e(StaticConfig.app_name, "IllegalAccessException", e);
} catch (IllegalArgumentException e) {
Log.e(StaticConfig.app_name, "IllegalArgumentException", e);
}
}
editor.apply();
}