[英]Android SharedPreferences gets cleared?
I'm using shared preferences as I always do, but recently in a new application the cache suddenly returns null, 我和往常一样使用共享首选项,但是最近在一个新应用程序中,缓存突然返回null,
here are the methods for read/write 这是读/写的方法
public static void saveToSharedPreferences(Context mContext, String key, String value) {
if (mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
if (mSharedPreferences != null)
mSharedPreferences.edit().putString(key, value).commit();
}
}
public static String readFromSharedPreferences(Context mContext, String key) {
if (mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
if (mSharedPreferences != null)
return mSharedPreferences.getString(key, null);
}
return null;
}
then in the code 然后在代码中
Utils.saveToSharedPreferences(getActivity(), mKey, mDATA);
in the same session when using 在同一会话中使用
String mDATA = Utils.readFromSharedPreferences(getActivity(), mKey);
it does return the value, but later on when exiting the app and lauching it again, it returns null, everything seems to be fine 它确实会返回值,但是稍后退出应用程序并再次启动它时,它返回null,一切似乎都很好
any help would be appreciated 任何帮助,将不胜感激
Are you sure the Context you're using is not NULL? 您确定使用的上下文不是NULL吗? Try this: 尝试这个:
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(key, value);
editor.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.