Storage data for Android development

Source: Internet
Author: User

sharedpreferences of Android data storageOne: Sharedpreferences
    • Sharedpreferences is a lightweight storage class on the Android platform that holds some of the most commonly used configurations of the app, such as activity state, when activity is paused, Saves the state of this activity to sharedpereferences, and when the activity is overloaded, the system callback method Onsaveinstancestate, and then takes the value out of the sharedpreferences. Sharedpreferences can be used to share data, including applications, or different components in the same application. For example, two activity can share data through sharedpreferences, in addition to passing data through Intent .
    • Four modes of operation for sharedpreferences data
    1. context.mode_private
    2. context.mode_append
    3. context.mode_world_readable
    4. context.mode_world_writeable context.mode_private: Is the default mode of operation, which means that the file is private and can only be accessed by the app itself, where the content written overwrites the contents of the original file context.mode_append: The mode checks whether the file exists, appends content to the file, or creates a new file. context.mode_world_readable and Context.mode_world_ Writeable is 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;
    • There are two ways to get sharedpreferences: 1 calling the Getsharedpreferences () method of the context object 2 Calling the Activity object's Getpreferences () method two different ways to differentiate: The Sharedpreferences object obtained by invoking the Getsharedpreferences () method of the context object can be shared by other components under the same application. The Sharedpreferences object obtained by invoking the Getpreferences () method of the activity object can only be used in that activity.
Two: Examples
    • This time write 2048 game, in which use Sharedpreferences to store the maximum value;
    • Data write: bestgrate is int type, when bestgrate is greater than 16, the data is stored;
    • Data is removed (only once):

Storage data for Android development

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.