Search code examples
androidtransfuse

How to retrieve SharedPreferences values with Transfuse


I've been playing around with Transfuse (http://androidtransfuse.org/) and am now trying to tackle SharedPreferences. The documentation makes this seem pretty straight forward:

@Activity
public class Example{
    @Inject @Preference(value = "favorite_color", default = "green")
    String favColor;
}

However, as I understand it, SharedPreferences are retrieved by name, not just by key. So, how does Transfuse know the name of the SharedPreferences file I'm trying to access?

I've tried something like this to no avail:

@Activity
public class MainActivity{
    public static final String PREF_NAME = "pref_name";

    @Inject
    android.app.Activity mActivity;

    @Inject @Preference(value = PREF_NAME, defaultValue = "")
    String mPreference;

    @Inject @View(R.id.preference)
    EditText mPreferenceEditText;

    @RegisterListener(R.id.button_2)
    android.view.View.OnClickListener mSavePrefListener = new android.view.View.OnClickListener() {
        @Override
        public void onClick(android.view.View v) {
            String val = mPreferenceEditText.getText().toString();
            mActivity.getSharedPreferences("the_shared_prefs", Context.MODE_PRIVATE)
                .edit()
                .putString(PREF_NAME, val)
                .apply();
        }
    };

    @OnResume
    private void displayPrefText(){
        mPreferenceEditText.setText(mPreference);
    }
}

Thanks for your help!


Solution

  • The @Preference injection uses the PreferenceManager.getDefaultSharedPreferences() method (as CommonsWare suggested) to look up the SharedPreferences object. This is a convenience for using the default preferences directly. Your example would basically generate the following injection:

    delegate.favColor = activity.getDefaultSharedPreferences()
                            .getString("favorite_color", "green");
    

    If you like, you can set up Transfuse to inject a specific SharedPreferences object via a Provider or @Provides method and qualifier:

    @TransfuseModule
    class Module{
        @Provides @Named("the_shared_prefs")
        public SharedPreferences build(Activity activity){
            return activity.getSharedPreferences("the_shared_prefs", Context.MODE_PRIVATE)
        }
    }
    

    Which you could then inject into your activity like so:

    @Activity
    public class MainActivity{
        public static final String PREF_NAME = "pref_name";
    
        @Inject @Named("the_shared_prefs")
        SharedPreferences theSharedPreferences;
    
        @Inject @View(R.id.preference)
        EditText mPreferenceEditText;
    
        @RegisterListener(R.id.button_2)
        android.view.View.OnClickListener mSavePrefListener = new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                String val = mPreferenceEditText.getText().toString();
                theSharedPreferences
                    .edit()
                    .putString(PREF_NAME, val)
                    .apply();
            }
        };
    }
    

    We may want to expand the @Preference injection to allow you to specify a non-default shared preferences. Would this help?