Using PreferenceFragment and Architecture

In my application, I have a preference: "pref1" with possible values ​​of 0, 1, 2. I keep them with the class SharedPreferences.

  • And I want to provide a user interface to change it. I am creating a class SettingFragment extends PreferenceFragmentand xml file with PreferenceScreenroot. In the xml file I have to set the field android:keyto bind UI and SharedPreferences.

  • To change the behavior of my application after the user changes "pref1", I need to implement OnSharedPreferenceChangeListener.

  • Also, when I start the program, I need to get the value "pref1" to synchronize the state of the program in accordance with the saved settings.

So, there are 3 places that I have to change when I want to add / remove / change the unit of setting of my application. It looks bad. Another option is to write classone that has a member variable of type SharedPreferencesand hide all SharedPreferenceskeys and give simple set / get methods to get the settings. But in this case, I have to use the usual tool for Android user interfaces: layouts.

Thus, you can use PreferenceFragmentit without spreading the knowledge about the internal structure of the preference store (key names, default values, their types) in several places, or is it better to use regular layouts?

+4
source share
2 answers

, 3 SharedPreference - 1. . 2. . 3. .

- , . , ( ), , . , UI building. Singleton ( ). , set/get. - , . singleton- API / . / , , .

+2

PreferenceFragment. Android , (android: persistent = true). true. :

PreferenceFragment Preference . SharedPreferences .

    <ListPreference
            android:key="pref1"
            android:title="title"
            android:summary="description"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:defaultValue="1"
            android:dialogTitle="dialog title" />

, entryvalues_list_preference.

, .

ListPreference1 = (ListPreference) findPreference ( "pref1" );

    ListPreference preference1 = (ListPreference) findPreference ("pref1");

    performAction(preference1.getValue());
    preference1.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    /**
     * Called when a Preference has been changed by the user. This is
     * called before the state of the Preference is about to be updated and
     * before the state is persisted.
     * 
     * @param preference The changed Preference.
     * @param newValue The new value of the Preference.
     * @return True to update the state of the Preference with the new value.
     */
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            performAction(String.valueOf(newValue));
            return true;
        }
    });

private void performAction(String type) {
    switch (type){
        case "0": // when 0 value is selected

            break;
        case "1": //when 1 value is selected

            break;
        case "2": //when 2 value is selected

            break;
    }
}

- , / xml / , Android .

0

Source: https://habr.com/ru/post/1670125/


All Articles