Shared Preferences of Android

Posted by tpstudent on Fri, 28 Jun 2019 01:08:12 +0200

Shared Preferences, as one of Android's data storage methods, is characterized by:
1. Only Java basic data types are supported, and custom data types are not supported.
2. Data sharing in application;
3. Easy to use.

Usage method

1. Storage of data

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
sp.edit().putString("name", "Xiao Zhang").putInt("age", 11).commit();


Or the following can be written as well

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("name", "Xiao Zhang");
editor.putInt("age", 11);
editor.commit();


Remember not to write in the following format, which will result in data being unable to store

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
sp.edit().putString("name", "Xiao Zhang");
sp.edit().putInt("age", 11);
sp.edit().commit();

 

Why can't it be stored in this way, because sp.edit() returns a new Editor object every time. There will be a cached Map in EditorImpl, the implementation class of Editor. At last, when commit, the cached Map will be written to the Map in memory, and then the Map in memory will be written to the XML file. Using commit in the above way, because sp.edit() returns a new Editor object again, the Map in the cache is empty, so the data cannot be stored.

2. Data acquisition

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
String name = sp.getString("name", null);
int age = sp.getInt("age", 0);


The implementation of getShared Preferences is frameworks/base/core/java/android/app/ContextImpl.java. The code is as follows:

@Override
public SharedPreferences getSharedPreferences(String name, int mode) {
    SharedPreferencesImpl sp;
    synchronized (ContextImpl.class) {
        ......
        final String packageName = getPackageName();
        ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
        if (packagePrefs == null) {
            packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
            sSharedPrefs.put(packageName, packagePrefs);
        }

        ......
        sp = packagePrefs.get(name);
        if (sp == null) {
            File prefsFile = getSharedPrefsFile(name);
            sp = new SharedPreferencesImpl(prefsFile, mode);
            packagePrefs.put(name, sp);
            return sp;
        }
    }
    ......
    return sp;
}


Shared Preferences Impl is the specific implementation class of Shared Preferences interface. A name corresponds to a Shared Preferences Impl. There are many Shared Preferences Impls in an application according to different names.
Shared Preferences Impl is implemented in frameworks/base/core/java/android/app/Shared Preferences Impl.java. We can get an instance of Shared Preferences through getShared Preferences. When we call get data from a map such as sp.getString, it is actually taken directly from memory. The first parameter of the get method is the key of the map, and the second parameter is the key of the map. The default value is returned when there is no corresponding key value in the Map.

Topics: Java Android xml