Android 偏好设置未保存
当用户更新(或者我猜是安装)我的应用程序时,我试图创建一个“新增内容”弹出对话框。我有以下逻辑来检查最后安装的版本,然后将其与当前版本进行比较,然后将当前版本保存为“最后”版本(这在我的“第一个”的 onCreate() 末尾调用“ Activity
):
int lastVersion = getPreferences(MODE_PRIVATE).getInt(LAST_VERSION, 34);
try {
int currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
Log.d(getClass().getSimpleName(), "Old Version = " + lastVersion);
Log.d(getClass().getSimpleName(), "New Version = " + currentVersion);
if (currentVersion != lastVersion) {
// write the current version to the preferences so they won't see the popup again
getPreferences(MODE_PRIVATE)
.edit()
.putInt(LAST_VERSION, lastVersion)
.commit();
// tell the user the new thing
AlertDialog dia = new AlertDialog.Builder(this)
.setTitle(R.string.main_menu_new_title)
.setMessage(R.string.main_menu_new_body)
.create();
dia.show();
}
} catch (NameNotFoundException ex) {}
问题是,无论我运行应用程序多少次,我总是会得到 lastVersion
的默认值,因此总是会出现弹出窗口。有想法吗?
I'm trying to create a "what's new" pop-up dialog when users update (or, I guess, install) my app. I have the following bit of logic to check that the last installed version was then compare it to the current version, then save the current version as the "last" version (this is called at the end of the onCreate() of my "first" Activity
):
int lastVersion = getPreferences(MODE_PRIVATE).getInt(LAST_VERSION, 34);
try {
int currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
Log.d(getClass().getSimpleName(), "Old Version = " + lastVersion);
Log.d(getClass().getSimpleName(), "New Version = " + currentVersion);
if (currentVersion != lastVersion) {
// write the current version to the preferences so they won't see the popup again
getPreferences(MODE_PRIVATE)
.edit()
.putInt(LAST_VERSION, lastVersion)
.commit();
// tell the user the new thing
AlertDialog dia = new AlertDialog.Builder(this)
.setTitle(R.string.main_menu_new_title)
.setMessage(R.string.main_menu_new_body)
.create();
dia.show();
}
} catch (NameNotFoundException ex) {}
The problem is that no matter how many times I run the app I always get the default value for lastVersion
, so the pop-up ALWAYS happens. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将旧版本号写回首选项。
更改:
至:
You're writing the old version number back to the preferences.
Change:
to: