How to use sharedPreferences for Android data sharing
SharedPreferences in Android to persist and share data
Use it in an Activity or Context environment
(name, Context.MODE_PRIVATE);
Set the data to be saved:
mSp = (name, Context.MODE_PRIVATE); mEditor = (); ("test", "abc"); ("test2", "def"); (); ("test", "defaultValue_1"); ("test2", "defaultValue_2");
Notice:
1. Get a new editor object through edit() to write. After the settings are completed, you must call the commit or apply method before it can be finally written to the disk file. Otherwise, the data will be lost after restarting the application!
2. You must use local variables to save the obtained edit() editor object, and you cannot operate every item through the edit() method, because every time you call the edit method, a new object instance will be generated, and the operation will not be the same object. I have been puzzled for a long time and found out after several tests.
3. Reading stored data is obtained through the SharedPreferences object itself, rather than the editor object.
4. The default value must be specified when obtaining data
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!