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?
source
share