FrioneL
FrioneL

Reputation: 941

Android initialization with SharedPreferences

I have a problem with SharedPrefences and initializations.

My application has a login where you insert an user, and for that user, you have a specific preferences... so, I need to save preferences according to the user to load it later.

I though that SharedPrefences would be the solution, and it really is I think, but I have a problem to initialize they: I have an Activity class called Options. It has static functions that returns the value of the options... but I have a problem, I call that functions before I have create that activity (intent), so I think that the functions are returning the last value that the last user has selected on the options...

How can I load the options before that calls?

I though to use first of all an Intent sending extra data with the user and in onCreate() of the Options, initialize they, but if I make an intent, then the Options will appear (xml will load).

Any help pls?

Upvotes: 2

Views: 4835

Answers (3)

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

I work on the dependency injector for android, and content of shared preferences is already injectable into annotated fields ( see: https://github.com/ko5tik/andject , PreferenceInjector). Patches and improvements are welcome. Saving of preferences comes soon

Upvotes: 0

SnowyTracks
SnowyTracks

Reputation: 1985

Try something like this.... adding methods for each variable you want to save.

 public class PreferenceManager {

    private static PreferenceManager self;
    private SharedPreferences preferences = null;
    private SharedPreferences.Editor editor = null;
    private boolean isInitialised = false;
    private static final String MY_PREFERENCE = "mypreferencekey";
    private String myPreference = null;

    public void initialise(Context context) {
    if (!isInitialised) {
        preferences = context.getSharedPreferences("MyPreferencePlace", Context.MODE_PRIVATE);
        editor = preferences.edit();
        loadPreferences();
        isInitialised = true;
    }
    }

    public static PreferenceManager getInstance() {
    if (self == null) {
        self = new PreferenceManager();
    }
    return self;
    }

    private PreferenceManager() {
    }

    public void setPreference(String newPreferenceValue) {
    this.myPreference = newPreferenceValue;
    savePreferences();
    }

    public String getPreference(){
    return myPreference;
    }

    private void savePreferences() {
    editor.putString(MY_PREFERENCE, myPreference);
    editor.commit();
    }

    private void loadPreferences() {
    myPreference = preferences.getString(MY_PREFERENCE, null);
    }

}

Upvotes: 3

Jordy Langen
Jordy Langen

Reputation: 3591

All the SharedPreferences need is a context and it can be initialized. As your application always opens an Activity to start with, you always have a context to work with.

I would advise you to wrap the SharedPreferences in a Singleton class and just pass a context as parameter at the getInstance method. You should be able to access your shared preferences at all Activities this way.

Upvotes: 0

Related Questions