Basic usage of sharedpreferences

Source: Internet
Author: User

There are two ways to get Sharedpreferences: 1 call the Getsharedpreferences () method of the context object to call the Getpreferences () method of the activity object in two ways: The Sharedpreferences object that is obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application. Invoke the Activity object's getpreferences () The Sharedpreferences object obtained by the method can only be used in this activity.  sharedpreferences Four modes of operation: Context.MODE_PRIVATEContext.MODE_ Appendcontext.mode_world_readablecontext.mode_world_writeable context.mode_private: The default mode of operation, which represents the file as private data, can only be accessed by the app itself, in which the contents of the write overwrite the contents of the original file Context.mode_append: The schema checks whether the file exists, appends content to the file, or creates a new file. Context.mode_world_readable and context.mode_world_writeable are used to control whether other apps have permission to read and write to the file. Mode_world_ Readable: Indicates that the current file can be read by another application. Mode_world_writeable: Indicates that the current file can be written by another application. Save data to Sharedpreferences:sharedpreferences Preferences=getsharedpreferences ("User", context.mode_private); Editor Editor=preferences.edit (); String name= "Xixi"; String age= "editor.putstring" ("Name", "name"), Editor.putstring ("age", age), Editor.commit ();  Get data from Sharedpreferences: Sharedpreferences preferences=getsharedpreferences ("User", context.mode_pRivate); String name=preferences.getstring ("name", "DefaultName"); String age=preferences.getstring ("Age", "0");

Basic usage of sharedpreferences

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.