共享首选项默认在首次运行应用程序时不绑定



我有一个首选项活动,其中包含用于货币选择的列表首选项。首选项的默认值为"CHF"。

但是,当我安装该应用程序并首次运行它时,它就像首选项未存储在共享首选项文件中一样。

主活动的文本视图取决于此首选项的值,因此即使在第一次运行应用程序时,我也需要绑定首选项。

在我的主要活动中,在 onResume 中,我调用以下方法:

private void bindMassageViews() {
    String currency = PreferenceManager.getDefaultSharedPreferences(getContext())
            .getString(getString(R.string.currency_preference_key), "");
    massageName.setText(mBuilder.getMassageName());
    priceContentTextview.setText(String.format(priceElement,
            String.valueOf(mBuilder.getPrice()), currency));
}

首选项本身如下:

<ListPreference
    android:defaultValue="@string/swiss_franc_symbol"
    android:entries="@array/pref_currency_values"
    android:entryValues="@array/pref_currency_values"
    android:key="@string/currency_preference_key"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/pref_title_currency" />

我的偏好活动是这样的:

公共类设置活动扩展 AppCompatPpreferencesActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);
    bindPreferences();
    setupActionBar();
}
/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // Show the Up button in the action bar.
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        if (!super.onMenuItemSelected(featureId, item)) {
            NavUtils.navigateUpFromSameTask(this);
        }
        return true;
    }
    return super.onMenuItemSelected(featureId, item);
}
/**
 * A preference value change listener that updates the preference's summary
 * to reflect its new value.
 */
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object value) {
        String stringValue = value.toString();
        if (preference instanceof ListPreference) {
            // For list preferences, look up the correct display value in
            // the preference's 'entries' list.
            ListPreference listPreference = (ListPreference) preference;
            int index = listPreference.findIndexOfValue(stringValue);
            // Set the summary to reflect the new value.
            preference.setSummary(
                    index >= 0
                            ? listPreference.getEntries()[index]
                            : null);
        } else {
            // For all other preferences, set the summary to the value's
            // simple string representation.
            preference.setSummary(stringValue);
        }
        return true;
    }
};
/**
 * Binds a preference's summary to its value. More specifically, when the
 * preference's value is changed, its summary (line of text below the
 * preference title) is updated to reflect the value. The summary is also
 * immediately updated upon calling this method. The exact display format is
 * dependent on the type of preference.
 *
 * @see #sBindPreferenceSummaryToValueListener
 */
private static void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes.
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    // Trigger the listener immediately with the preference's
    // current value.
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
            PreferenceManager
                    .getDefaultSharedPreferences(preference.getContext())
                    .getString(preference.getKey(), ""));
}

private void bindPreferences() {
    bindPreferenceSummaryToValue(findPreference(getString(R.string.currency_preference_key)));
}
}

如何确保在首次运行应用程序时绑定首选项的默认值?

复制我的评论,这有助于解决问题:

尝试在onCreate中添加PreferenceManager.setDefaultValues(this, R.xml.preference, false);,看看它是否会改变任何内容

最新更新