【问题标题】:Why shared preferences has been reset up on app crash or force close Android为什么共享首选项已在应用程序崩溃或强制关闭 Android 时重置
【发布时间】:2014-11-28 13:30:51
【问题描述】:

我已获取 CSIPSIMPLE 代码并重新打包到 com.mycompany.appname

问题是,每当应用程序崩溃时,所有值都会从共享首选项中删除。

为什么?

我的申请是

public class BeemApplication extends Application {

    static BeemApplication application = null;

    public static SharedPreferences mPref;
    public static Editor mEditor;

    public BeemApplication() {

    }

    public static BeemApplication getInstance() {
        if(application != null) {
            return application;
        } else {
            return new BeemApplication();
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        mEditor = mPref.edit();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}

在活动中我会得到他们喜欢的,

BeemApplication.mEditor.putString(ctx.getString(R.string.pref_online_number), number).commit();

BeemApplication.mPref.getString(ctx.getString(R.string.pref_online_number), number).commit();

【问题讨论】:

  • crashes的情况下发布logcat
  • “所有值都从共享首选项中删除”——您对此有何证据?
  • 确保没有使用 null 键保存您的任何首选项,因为在您的应用尝试加载preferences.xml文件时崩溃后,它可以导致首选项崩溃并且首选项将被重置。
  • 记得调用 .commit()
  • @CommonsWare,当我试图获取存储的值时,它会给出默认值

标签: android sharedpreferences


【解决方案1】:

这是包括我自己在内的许多人都面临的常见问题。 看看这篇分享你的发现的帖子Android - Shared Preferences are lost sometimes

我建议不要跨共享首选项存储持久数据,而是使用数据库表之类的东西来存储设置。

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    相关资源
    最近更新 更多