Two ways to get Sharedpreference objects in Android

Source: Internet
Author: User

When we save some small amount of data, especially some key,value this kind of data is saved in sharedpreferences, there are two ways to get Sharedpreferences on Android,

1: Acquired through context contexts, ctx.getsharedpreferences

2: Via Preferencemanager.getdefaultsharedpreferences (CTX)

But these two kinds of data to save the file name is not the same, the first file name is its own definition, the second is the system to write dead, is packagename+"_preferences.xml",

Now look at the source:

public static Sharedpreferences Getdefaultsharedpreferences (context context) {
Return Context.getsharedpreferences (Getdefaultsharedpreferencesname (context),
Getdefaultsharedpreferencesmode ());
}

Getdefaultsharedpreferencesname (context) method:

private static String Getdefaultsharedpreferencesname (context context) {
return Context.getpackagename () + "_preferences";
}


private static int Getdefaultsharedpreferencesmode () {
return context.mode_private;
}

This is the source code is simple, not much to say

Two ways to get Sharedpreference objects 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.