Csaba Kozák edited this page Mar 16, 2016 · 22 revisions

Since AndroidAnnotations 2.1

SharedPreferences helpers allow you to use Android SharedPreferences, but in a typesafe manner, instead of using strings.

Defining the preferences

First, you should create an interface annotated with @SharedPref to define the SharedPreferences :

public interface MyPrefs {

        // The field name will have default value "John"
	String name();

        // The field age will have default value 42
	int age();

        // The field lastUpdated will have default value 0
	long lastUpdated();


Based on that specification, AndroidAnnotations builds a SharedPreferences Helper that has the same name plus an underscore. You can get an instance of the generated helper in any enhanced class with the @Pref annotation.

Important: The type of the field MUST be the generated class instead of the source class. It's the only exception in AA.

public class MyActivity extends Activity {

	MyPrefs_ myPrefs;
	// ...


Method based injection

Since AndroidAnnotations 4.0.0

public class MyActivity extends Activity {

  void setOnePref(MyPrefs_ myPrefs){
    // do something with myPrefs
  void setMultiplePrefs(@Pref MyPrefs_ myPrefs, @Pref AnotherPrefs_ anotherPrefs){
    // do something with myPrefs and anotherPrefs


You can then start using it:

// Simple edit"John");

// Batch edit

// Preference clearing:

// Check if a value exists:
boolean nameExists =;

// Reading a value
long lastUpdated = myPrefs.lastUpdated().get();

// Reading a value and providing a fallback default value
long now = System.currentTimeMillis();
long lastUpdated = myPrefs.lastUpdated().getOr(now);

Default resource value

Since AndroidAnnotations 3.0

It's now possible to inject a default value from Android resources with @DefaultRes:

public interface MyPrefs {
    String resourceName();

    @DefaultRes // uses 'R.string.defaultPrefAge' to set default value
    String defaultPrefAge();

String resource as preference key

Since AndroidAnnotations 3.1

You can specify a String resource by its id to be used as the preference key instead of the method name. This is useful when you specify your preferences in an xml file, and you use String resource keys there. Example:

public interface MyPrefs {
    @DefaultString(value = "John", keyRes = R.string.myPrefKey)
    String name();

    @DefaultRes(keyRes = R.string.myOtherPrefKey)
    String defaultPrefAge();


Observe that you can name the shared preference by setting value to one of the following:

  • ACTIVITY, for a shared preference named MyActivity_MyPrefs;
  • ACTIVITY_DEFAULT, for a shared preference named MyActivity (also available through activity.getPreferences());
  • APPLICATION_DEFAULT, for the default SharedPreference or UNIQUE, for a shared preference named MyPrefs.

Therefore, if a single shared preference is needed for the interface defined, in order to all activities of a given application to share the same preferences, the following should be used:

public interface MyPrefs {

Using it with a PreferenceActivity

The Android PreferenceActivity or PreferenceFragment can edit the values of your shared preferences.

public interface MyPrefs {
public static String PREF_NAME = "MyPrefs";

// in onCreate

// Using your MyPrefs values 

// Opening the layout 

You may want to check out the @Preference API helpers.

14/10/2018 The 4.5.1 release is out !

Using AndroidAnnotations


Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.