更改升级时的默认首选项
change default preference on upgrade
在我的应用程序中,我有以下代码告诉我某项功能是否默认启用:
public boolean getFeatureEnabled()
{
return mPrefs.getBoolean("FEATURE_ENABLED", DEFAULT_ENABLED);
}
仅当用户从 UI 更改设置时,此首选项才会被覆盖。所以默认情况下它从 DEFAULT_ENABLED
中获取值,这是某个地方的 class 变量。
在当前版本中,DEFAULT_ENABLED
是 true
,但在我的应用程序的下一版本中将是 false
。
问题是更新后,使用上面的代码,没有更改默认设置的老用户 UI 将禁用他们的功能 - 我想避免这种情况。
关于如何处理这个问题有什么建议吗?
在新版本的 preferences
中将另一个标记“FIRST_TIME
”作为“true
”。检查您应用的第一个屏幕
if(FIRST_TIME==true)
{
//put FEATURE_ENABLED = false;
//put FIRST_TIME = false;
}
这样做 FEATURE_ENABLED
将在用户首次启动应用时设置为 false,并且不会考虑默认值
据我了解,您有一个默认启用的功能,但此默认值从未写入 SharedPreferences
,除非用户明确更改。
现在您希望默认情况下禁用该功能,但不影响已启用它的用户的行为。
我能想到3个选项:
选项 1 如果您已经保存了最后一个版本,您可以在迁移逻辑中检查:
private void migratePreferences(Context context) {
SharedPreferences prefs = context.getSharedPreferences("your_preference_file", MODE_PRIVATE);
int lastKnownVersionCode = (prefs.getInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE);
prefs.edit().putInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE).apply();
//this is the old featureEnabled check
boolean oldPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", true);
boolean newPreferenceValue;
if (prefs.contains("FEATURE_ENABLED")) {
//the feature was modified by the user so respect their preference
newPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", false);
} else if (lastKnownVersionCode == BUGGY_VERSION_WITH_FEATURE_ENABLED_BY_DEFAULT) {
//the user is updating from the buggy version.
// this check could include a range of versions if you've released several buggy versions.
// this is also where option 2 would be inserted
newPreferenceValue = oldPreferenceValue;
} else {
//the new default that will apply to fresh installs
newPreferenceValue = false;
}
//save the preference
prefs.edit().putBoolean("FEATURE_ENABLED", newPreferenceValue).apply();
}
但这取决于您已经在您的应用程序启动代码中的某处调用了此方法。
选项 2 如果你不这样做,还有希望。您可以使用给出的答案检查这是否是您的第一次安装 in this Whosebug answer
选项 3 您可以发布应用的中间版本,它的行为与现在相同,但将未保存的默认设置保存在 SharedPreferences
中。这将为热切的用户保留该功能的原样,但您必须等到大部分用户更新后才能发布所需的行为。
在我的应用程序中,我有以下代码告诉我某项功能是否默认启用:
public boolean getFeatureEnabled()
{
return mPrefs.getBoolean("FEATURE_ENABLED", DEFAULT_ENABLED);
}
仅当用户从 UI 更改设置时,此首选项才会被覆盖。所以默认情况下它从 DEFAULT_ENABLED
中获取值,这是某个地方的 class 变量。
在当前版本中,DEFAULT_ENABLED
是 true
,但在我的应用程序的下一版本中将是 false
。
问题是更新后,使用上面的代码,没有更改默认设置的老用户 UI 将禁用他们的功能 - 我想避免这种情况。
关于如何处理这个问题有什么建议吗?
在新版本的 preferences
中将另一个标记“FIRST_TIME
”作为“true
”。检查您应用的第一个屏幕
if(FIRST_TIME==true)
{
//put FEATURE_ENABLED = false;
//put FIRST_TIME = false;
}
这样做 FEATURE_ENABLED
将在用户首次启动应用时设置为 false,并且不会考虑默认值
据我了解,您有一个默认启用的功能,但此默认值从未写入 SharedPreferences
,除非用户明确更改。
现在您希望默认情况下禁用该功能,但不影响已启用它的用户的行为。
我能想到3个选项:
选项 1 如果您已经保存了最后一个版本,您可以在迁移逻辑中检查:
private void migratePreferences(Context context) {
SharedPreferences prefs = context.getSharedPreferences("your_preference_file", MODE_PRIVATE);
int lastKnownVersionCode = (prefs.getInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE);
prefs.edit().putInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE).apply();
//this is the old featureEnabled check
boolean oldPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", true);
boolean newPreferenceValue;
if (prefs.contains("FEATURE_ENABLED")) {
//the feature was modified by the user so respect their preference
newPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", false);
} else if (lastKnownVersionCode == BUGGY_VERSION_WITH_FEATURE_ENABLED_BY_DEFAULT) {
//the user is updating from the buggy version.
// this check could include a range of versions if you've released several buggy versions.
// this is also where option 2 would be inserted
newPreferenceValue = oldPreferenceValue;
} else {
//the new default that will apply to fresh installs
newPreferenceValue = false;
}
//save the preference
prefs.edit().putBoolean("FEATURE_ENABLED", newPreferenceValue).apply();
}
但这取决于您已经在您的应用程序启动代码中的某处调用了此方法。
选项 2 如果你不这样做,还有希望。您可以使用给出的答案检查这是否是您的第一次安装 in this Whosebug answer
选项 3 您可以发布应用的中间版本,它的行为与现在相同,但将未保存的默认设置保存在 SharedPreferences
中。这将为热切的用户保留该功能的原样,但您必须等到大部分用户更新后才能发布所需的行为。