数据得到我使用一些代码在活动的onActivityResult方法来更新数据:按钮不更新从SharedPreference
//save state
editorUserInfoSharedPreferences.putString("userType", productId);
editorUserInfoSharedPreferences.putInt("paymentStatus", 1);
editorUserInfoSharedPreferences.commit();
editorUserInfoSharedPreferences.apply();
int paymentStatus = userInfoSharedPreferences.getInt("paymentStatus", 0);
String userType = userInfoSharedPreferences.getString("userType", "");
Log.e(TAG, "paymentStatus 1700: "+ paymentStatus);
Log.e(TAG, "userType 1700: "+ userType);
在日志中的数据是正确的。但在那之后,我打一个按钮,并从sharePreference获取数据:
int paymentStatus = userInfoSharedPreferences.getInt("paymentStatus", 0);
String userType =
userInfoSharedPreferences.getString("userType", "");
Log.e(TAG, "paymentStatus 1532: "+ paymentStatus);
Log.e(TAG, "userType 1530: "+ userType);
中的数据记录是旧数据。 这是我的日志:
E/MAINACTIVITY: paymentStatus 1700: 1
E/MAINACTIVITY: userType 1700: monthly_0
....
E/MAINACTIVITY: paymentStatus 1532: 0
E/MAINACTIVITY: userType 1530: 0
我该如何解决这个问题?提前感谢你!
您应该使用提交或应用 –
如果我同时使用它们,是否有问题? –
是的,其中一个是同步的,另一个是异步的。另外android建议使用apply()(同步),如果你正在使用多线程,并且你正在使用main.thread,你可以使用commit。 –