我采用了 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();
}
}
在 Activity 中我会让他们喜欢,
BeemApplication.mEditor.putString(ctx.getString(R.string.pref_online_number), number).commit();
BeemApplication.mPref.getString(ctx.getString(R.string.pref_online_number), number).commit();
最佳答案
这是包括我在内的许多人都面临的常见问题。 看看这个帖子Android - Shared Preferences are lost sometimes分享您的发现。
我建议不要跨共享首选项存储持久数据,而是使用数据库表之类的东西来存储设置。
关于android - 为什么在应用程序崩溃或强制关闭 Android 时已重置共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27189791/