Detailed explanation of sharedperference storage for Android learning

Sharedperference is different from file storage. It uses key value to store data. For each piece of saved data, a key value will be given, so that the corresponding data can be directly retrieved through the key value when reading data. Amdroid provides three methods to obtain instances:

1. Getsharepreferences () method in context class

It receives two parameters, the first is the file name; The second is the operation mode. At present, there is only mode_ Private is optional. This is the default operation mode, which means that only the current application can operate on files.

2. Getpreference () method in activity class

It only receives one operation mode parameter, because using this method will automatically take the class name sharedpreference as the file name.

3. Getdefaultsharedpreference() method in preferencemanager class

It is mainly realized in three steps:

(1) Call the edit () method of the SharedPreferences object to get a SharedPreferences. Editor object. (2) To add data to the SharedPreferences. Editor object, such as adding a Boolean data, use the putboolean () method to push in turn. (3) Call the apply () method to submit the added data to complete the data operation`

Use case

MainActivity:

Layout file

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>