简体   繁体   English

Android SharedPreferences是否已清除?

[英]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.

相关问题 Android未清除SharedPreferences - SharedPreferences not being cleared android 在Android中未清除sharedpreferences - sharedpreferences is not being cleared in android 如果从最近的活动android中删除应用程序,则SharedPreferences被清除 - SharedPreferences cleared if remove the app from recent activity android Arraylist在Android中自行清除 - Arraylist gets cleared on its own in android sharedpreferences中的值不会清除 - Values in sharedpreferences are not cleared Android 自定义 ListView - 当软键盘隐藏时 Edittext 被清除 - Android custom ListView - Edittext gets cleared when the soft keyboard hides 在android中添加新数据时,ArrayList数据被清除 - ArrayList data gets cleared when adding new data in android 在 Manifest 中使用 android:allowBackup="false" 时,在更新到新版本时会清除 sharedpreferences 吗? - sharedpreferences are cleared on update to new version when using android:allowBackup="false" in Manifest? 在 AndroidManifest 中使用 android:allowBackup="false" 时,在更新到新版本时会清除 sharedpreferences 吗? - sharedpreferences are cleared on update to new version when using android:allowBackup="false" in AndroidManifest? 运行 npx react-native run-android 时是否清除了 SharedPreferences? - Are SharedPreferences cleared when running npx react-native run-android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM