Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android default values for shared preferences

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

like image 419
Loric- Avatar asked Jul 19 '13 19:07

Loric-


People also ask

How is data stored in shared preferences?

Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.

How much data can shared preferences store?

There's no hard limit. The main reason it is not recommended to use SharedPreferences in place of the database is mainly the performance -> shared preferences data is keept in ordinary flat XML file which lacks all the features SQLite offers.

How does shared preferences work Android?

A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. Each SharedPreferences file is managed by the framework and can be private or shared. This page shows you how to use the SharedPreferences APIs to store and retrieve simple values.

Which among the following is the default mode of operation for the SharedPreferences?

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application.


1 Answers

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).

like image 179
Tobiel Avatar answered Sep 20 '22 18:09

Tobiel