Shared preferences in Android implements the function of storing user names

1. Introduction

SharedPreferences is a lightweight data storage method, which stores data in XML files through key value pairs. It is often used to store simple configuration information.

2. Usage

2.1 get SharedPreferences object

The SharedPreferences object can be obtained in android in the following three ways:

2.2.1 getsharedpreferences() in context class

Receive two parameters. The first parameter specifies the file to store data. If the specified file does not exist, the file will be created. The storage directory is "/ data / data / package_name / shared_prefs /, where package_ Name is the package name.

The second parameter is the operation mode, which mainly includes two types:

MODE_ Private: private mode, which is the default mode. It has the same effect as directly passing 0 as a parameter, indicating that only the current program can operate on this file.

MODE_ MULTI_ Process: multi process mode, which allows multiple processes to operate on the file.

2.2.2 getpreferences() in activity class

This method is similar to the previous method, except that it only receives one parameter to specify the operation mode without specifying the file name. By default, this method takes the class name of the current activity as the file name for storing data.

2.2.3 getdefaultsharedpreferences() in preferencemanager class

This is a static method that receives a context parameter and uses the package name of the current application as the file name for storing data.

2.2 get sharedpreferences.editor object

The SharedPreferences object itself can only read but not save data. If you need to save data, you need to call the edit () method of the SharedPreferences object to obtain an editor object.

2.3 storing data through putxxx method

After you get the editor object, you can call its putxxx method to add data. Here XXX refers to the added data type. For example, if you store string data, you can call the putstring () method. This method receives two parameters. The first parameter is the key value and the second parameter is the data value, that is, a key value pair.

2.4 submission of changes

After adding or removing (remove method) data, you need to call the commit () method of the editor object to submit the changes.

2.5 obtaining stored data

It is easy to obtain the stored data. You can directly call the getxxx method of the SharedPreferences object, which is similar to the putxxx method of the editor object. This method also receives two parameters. The first parameter specifies the key value of the data to be obtained, and the second parameter specifies the default value returned when the obtained data does not exist.

3. Example - realize the function of saving user name

Layout:

Activity class:

This article is for learning and communication. If there are mistakes, please correct them! I hope it will help you in your study, and I also 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
分享
二维码
< <上一篇
下一篇>>