- Create a class and annotate it with
@PrefData
@PrefData
public abstract class UserSettings {
// the fields must be protected or package private
int age;
String name;
}- Create an instance of the generated class (it will be prefixed by "Prefs")
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
PrefUserSettings userSettings = new PrefUserSettings(prefs);apt 'org.xelevra.libs:prefdata-processor:2.0'
provided 'org.xelevra.libs:prefdata-annotations:2.0'int age = userSettings.getAge();
userSettings.setAge(18);userSettings.setAge(18).setName("Stephen");Initial value of your fields will be default values.
int age = 18;For clearing the preferences use clear()
Mark the class with @GenerateRemove and remove methods will be generated
userSettings.removeAge();Mark your field with @Prefixed annotation
@Prefixed
int childAge;Then use
userSettings.setChildAge("James", 13);
userSettings.setChildAge("Anna", 15);userSettings.edit().setAge(18).setName("Stephen").apply();Note
Until you not called edit() all values will be saved immediatly.
When you call edit() all next settings will be saved after calling apply() or commit()
Example:
userParams.edit().setAge(32).setName("Bob").apply();Mark the field with @Keyword for custom SharedPreferences key. For example it might help you to migrate to the library from manual preferences setting.
@Keyword("NAME");
String name;Already supported only primitive types, its boxings and String
For the limiting possible values for field, use @Belongs annotation.
Example:
@Belongs("animals", "plants", "fungi", "chromista", "protist")
String eukaryoteKingdom
@Belongs("2", "3", "5", "7")
int smallPrimeNumber
@Belongs("0", "999999999999")
long npePerProject
@Belongs("-1", "0.43", "54.444f")
float randomNumberNote that following examples will not compile:
@Belongs("100", "50.5", "I don't know", "false", "90000000000")
int playerHp
//since floats, strings, booleans and longs are not allowed for ints
@Belongs("100", "50.5", "I don't know", "false", "90000000000")
long playerHp
//since floats, strings, booleans are not allowed for longs
@Belongs("100", "50.5", "I don't know", "false", "90000000000")
boolean whyIParticipatingInThis
//since only "true" and "false" allowed for booleans
@Belongs("100", "50.5", "I don't know", "false", "90000000000")
float playerHp
//since strings, booleans are not allowed for floatsThe library covers another important task you might need: set up some settings to the test builds without rebuilding. Usually programmers includes a special screen with the list of settings, and a tester should do some tricky actions to open it. The library let you take your settings out and manage them using special application provided with it.
- Mark the class or aspecial fields with
@Exportable - Extend the abstract class
PreferencesContentProvider
public class UserSettingsProvider extends PreferencesContentProvider {
@Override
protected Exporter getExporter() {
return new PrefUserSettings(getContext().getSharedPreferences("main", Context.MODE_PRIVATE));
}
}- Register it in your manifest
<provider
android:name=".UserSettingsProvider"
android:authorities="org.xelevra.prefdata.com.example.test"
android:exported="true"/>Important In authorities you must write exactly the line started with "org.xelevra.prefdata." and end with your package name. Otherwice the browser app won't find your provider.