How do you refresh PreferenceActivity to show changes in the settings?

前端 未结 10 1776
情深已故
情深已故 2020-12-14 15:52

Based on the following code, can you tell me how to refresh the PreferenceActivity window to show changes in the settings immediately? For example: the user taps the master

10条回答
  •  既然无缘
    2020-12-14 16:33

    Nikolay's answer is correct. I just want to add some code here to illustrate his point more clearly.

    private CheckBoxPreference mOn15Past;
    private CheckBoxPreference mOn30Past;
    private CheckBoxPreference mOn45Past;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    
        mOn15Past = (CheckBoxPreference) findPreference("ChimeOn15Past");
        mOn30Past = (CheckBoxPreference) findPreference("ChimeOn30Past");
        mOn45Past = (CheckBoxPreference) findPreference("ChimeOn45Past");
    
        final Preference chimeMaster = findPreference("ChimeMaster");
        chimeMaster.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newVal) {
                final boolean value = (Boolean) newVal;
                mOn15Past.setChecked(value);
                mOn30Past.setChecked(value);
                mOn45Past.setChecked(value);
                return true;
            }
    
        });
    }
    

    In short, PreferenceActivity is not designed to refresh its values from the persistent storage once it is started. Instead of using SharedPreferences.Editor to modify and commit additional changes like you did in your code, it is better to make the changes into the local PreferenceManager object which will be committed by the PreferenceActivity in its normal lifecycle.

提交回复
热议问题