Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Preferences

Tolik Pylypchuk edited this page Oct 28, 2017 · 3 revisions

If you need to get preferences, its as easy as injecting PreferencesService and calling its get method. It will return a strongly-typed preferences object instead of a map. The service does not provide the update functionality, because preferences are updated only in the preferences activity.

It should be easy to wire PreferencesService with PreferenceFragment. Although, a few adjustments need to be done.

Firstly, you need to inject PreferencesService into the fragment.

Secondly, preferences info is stored in res/xml/preferences.xml. This file describes which preferences are stored for the app. The fragment should load preferences info from this file.

Thirdly, the 'regions' preference is created dynamically, because the regions are stored in the database. That's why in the fragment's onCreate you should add this:

preferencesService.populateRegions((MultiSelectListPreference)findPreference("regions"));

Lastly, the service assumes that the name of the preferences file, used by the fragment is the same as the value of PreferencesService.PREFERENCES_NAME. To set the preference file, you should add this in the fragment's onCreate:

getPreferenceManager().setSharedPreferencesName(PreferencesService.PREFERENCES_NAME);

Bear in mind, that PreferencesService is completely untested, so if the need to change it arises, feel free to do so.

Clone this wiki locally