My Android growth path (11) - Detailed usage of Android's SharedPreferences

Detailed usage of Android's SharedPreferences :

1. Store data

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
sp.edit().putString("name", "小张").putInt("age", 11).commit();
 
  
  
  • 1
  • 2
  • 1
  • 2

Or the following can also be written

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("name", "小张");
editor.putInt("age", 11);
editor.commit();
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

Remember not to write in the following form, which will cause the data to be unable to be stored

SharedPreferences sp = getSharedPreferences("sp_demo", Context.MODE_PRIVATE);
sp.edit().putString("name", "小张");
sp.edit().putInt("age", 11);
sp.edit().commit();
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

Why this method cannot be stored, because sp.edit() will return a new Editor object every time, and there will be a cached Map in the Editor implementation class EditorImpl, and the Map in the cache will be written into the memory at the end of the commit The Map, and then write the Map in the memory into the XML file. Using the above method to commit, since sp.edit() returns a new Editor object, the Map in the cache is empty, so the data cannot be stored.

2. Take data

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

The specific implementation of getSharedPreferences is in 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;
}
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

SharedPreferencesImpl是SharedPreferences接口的具体实现类,一个name对应一个SharedPreferencesImpl,一个应用程序中根据name的不同会有多个SharedPreferencesImpl。 
SharedPreferencesImpl的具体实现是在frameworks/base/core/java/android/app/SharedPreferencesImpl.java,我们可以通过getSharedPreferences获得SharedPreferences的实例,当我们调用sp.getString等get方法取数据时,实际上是直接从内存中的Map里面去取,get方法传入的第一个参数正好是Map的key,第二个参数是当Map中没有这个key对应值的时候,返回的默认值。


Guess you like

Origin blog.csdn.net/cuper_/article/details/61196134