How to use shared preferences for Android data sharing

How to use shared preferences for Android data sharing

In Android, shared preferences is used to persistently store and share data

It can be used in the activity or context environment

Set data to save:

be careful:

1. Get a new editor object through edit () to write. After setting, you must call the commit or apply method to write it to the disk file, otherwise the data will be lost after restarting the application!

2. Local variables must be used to save the obtained edit () editor objects, but not every item can be operated through the edit () method, because each call of the edit method will generate a new object instance, and the operation will not be the same object. I have been puzzled for a long time and found it after several tests.

3. Reading the stored data is obtained through the SharedPreferences object itself, not 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 for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>