//以下为工具类代码,用于保存设置项,在应用内数据均可正常使用,退出应用再进去后设置项数据全取了默认值,求助问题出在哪里
public class SpUtil {
private static SharedPreferences sp;
public static void putBoolean(Context ctx,String key,boolean value){
if (sp==null){
sp=ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key,value).commit();
}
public static boolean getBoolean(Context ctx,String key,boolean defValue){
if (sp==null){
sp=ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
return sp.getBoolean(key,defValue);
}
public static void putString(Context ctx,String key,String value){
if (sp==null){
sp=ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
sp.edit().putString(key, value).commit();
}
public static String getString(Context ctx,String key,String defValue){
if (sp==null){
sp=ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
return sp.getString(key, defValue);
}
public static void remove(Context ctx, String key) {
if (sp==null){
sp=ctx.getSharedPreferences("config",Context.MODE_PRIVATE);
}
sp.edit().remove(key).commit();
}
}