android default values for shared preferences
Asked Answered
P

3

8

I am trying to understand the SharedPreferences of Android. I am a beginner and don't know a lot about it.

I have this class I implemented for my app Preferences

public class Preferences {
    public static final String MY_PREF = "MyPreferences";

    private SharedPreferences sharedPreferences;
    private Editor editor;

    public Preferences(Context context) {
        this.sharedPreferences = context.getSharedPreferences(MY_PREF, 0);
        this.editor = this.sharedPreferences.edit();
    }

    public void set(String key, String value) {
        this.editor.putString(key, value);
        this.editor.commit();
    }

    public String get(String key) {
        return this.sharedPreferences.getString(key, null);
    }

    public void clear(String key) {
        this.editor.remove(key);
        this.editor.commit();
    }

    public void clear() {
        this.editor.clear();
        this.editor.commit();
    }
}

The thing is that I would like to set default preferences. They would be set when the app is installed and could be modified after by the application and stay persistent. I heard about a preferences.xml but I don't understand the process.

Could someone help me?

Thanks for you time

Pintsize answered 19/7, 2013 at 19:14 Comment(0)
E
14

Simple, if you want a separate default value for each variable, you need to do it for each one, but on your method:

 public String get(String key) {
    return this.sharedPreferences.getString(key,"this is your default value");
}

If the variable was never accessed by the user or was never created, the system will set the default value as value and if you or the user changed this value, the default value is ignored. See http://developer.android.com/guide/topics/data/data-storage.html#pref

Directly from the Android Documentation:

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

Eustazio answered 19/7, 2013 at 23:34 Comment(2)
Thanks for your comment, I think it's what I need but is it possible to load this default value from a file? Or somewhere else that in the code?Pintsize
Absolutely. You could encode your default values in an XML or a text file and read it in your activity. You could also define your defaults as variables in another class and use those, possibly by passing them as parameters.Melly
M
0

Could you use the default value parameter of the getX() method?

For example, to get a String called 'username', you could use this:

String username = prefs.getString("username_key", "DefaultUsername");

You can simply define your default values in your Preferences class.

Melly answered 19/7, 2013 at 19:18 Comment(1)
Yeah it could be a solution but is it possible not to define those preferences in the code but in a file?Pintsize
A
0

You can store default values in string resource:

<string name="key_name">default_value</string>

and then get it as it follows:

int ResId = context.getResources().getIdentifier(key_name, "string", context.getPackageName()));
prefs.getString(key_name,context.getResources().getString(ResId);
Archaimbaud answered 8/5, 2015 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.