How to use Sharedpreferecences in Android

Source: Internet
Author: User

    • The role of Sharedpreferences

Storage of temporary data, to ensure that the next time access to the system, to obtain the data of the file, to operate.

    • Sharedpreferences the process of storing data and reading data:
    Storage data Information       1) Open a configuration file named Config, open it if it exists, or create a new configuration file named configuration         &N Bsp           sharedpreferences  sharedpreferences = getsharedpreferences ("Configuration", 0) ;                 getsharedpreferences (first parameter, second parameter);      &NB Sp           FIRST parameter: Storage file name second parameter: Open mode can be generated by the context call mode       2) Keep sharedpreferences in edit               Editor  editor  =   Sharedpreferences.edit ();     3) store data              editor.putstring ("      Name "," Harvey ");     4) Complete the submission             editor.commit (); Read data information       1) Open config file named configuration        sharedpreferences   Sharedpreferences = getsharedpreferences ("Configuration", 0);      2) Get data        string name = sharedpreferences.getstring ("name", "Default value");
    • Note:
sharedpreferences pre = context.getsharedpreferences (String name,int mode);name is the configuration file name for this component (if you want to share this profile with other components of the application, you can use this name to retrieve this profile, which you should pay special attention to, because the sharedpreferences has been identified in Android is saved as XML, so when you fill in the file name parameter, do not give the ". xml" suffix, and Android will automatically add it. Just write the file name directly. It is stored directly under the/data/data/<package name>/shared_prefs path, which is a key-value pair that holds the parameters. When you need to get a parameter value, follow the key index of the parameter.

How to use Sharedpreferecences in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.