【发布时间】:2017-03-16 07:49:06
【问题描述】:
我的共享首选项有问题,我有两个活动,这是我的共享首选项的代码。
public class SaveSharedPreferences {
static final String PREF_USER_NAME= "";
static final String PREF_PROPIC= "";
static SharedPreferences getSharedPreferences(Context ctx) {
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void setUserName(Context ctx, String userName)
{
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_USER_NAME, userName);
editor.commit();
}
public static String getUserName(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_USER_NAME, "");
}
public static void setProfile(Context ctx, String profile)
{
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.putString(PREF_PROPIC, profile);
editor.commit();
}
public static String getProfile(Context ctx)
{
return getSharedPreferences(ctx).getString(PREF_PROPIC, "");
}
public static void clearPrefs(Context ctx){
SharedPreferences.Editor editor = getSharedPreferences(ctx).edit();
editor.clear();
editor.commit();
}
}
每次我登录我的主要活动到下一个活动时,我总是向“PREF_USER_NAME”添加一个字符串值,它会如您在上面看到的那样存储。所以当我成功登录时,我调用“PREF_PROFILE”没有任何价值。但是在我调用它的时候,我得到的值是来自“PREF_USER_NAME”的值。所以这就是我的问题,我看不出有什么问题。所以有人可以帮助我,我感谢您的 cmets 和建议谢谢!
【问题讨论】:
标签: android login get set sharedpreferences