「已注销」 2016-09-19 12:04 采纳率: 0%
浏览 5702
已结题

存储在sharedPreference文件中的数据在退出应用后丢失

//以下为工具类代码,用于保存设置项,在应用内数据均可正常使用,退出应用再进去后设置项数据全取了默认值,求助问题出在哪里
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();
}

}

  • 写回答

4条回答

  • 小小亠星辰 2016-09-19 12:18
    关注

    这段代码没问题啊,给我看看你调用的那一块吧

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧