Android Preferences (4) Set default values

Source: Internet
Author: User

Setting Default Values

The preferences you create probably define some important behaviors for your application, so it's necessary that you Initi Alize The associated file with default values for each when the SharedPreferences Preference user first opens your application.

The first thing you must does is specify a default value for each Preference object in your XML file using the android:defaultValue attribute. The value can be any data type, which is appropriate for the corresponding Preference object. For example:

default boolean --><checkboxpreference    android:defaultvalue= "true"    Default value is a string--><listpreference    android:defaultvalue= "@string/pref_ Syncconnectiontypes_default "    />

Then, from the onCreate() method in your application ' s main activity-and in any other activity through which the user may Ente R your application for the first Time-call setDefaultValues() :

Preferencemanager.  Setdefaultvalues(this, R.  XML.  Advanced_preferences,false);            

Calling this during onCreate() ensures the your application is properly initialized with default settings, which your Applica tion might need to read in order to determine some behaviors (such as whether to download data and on a cellular network ).

This method takes three arguments:

    • Your application Context .
    • The resource ID for the preference XML file for which you want to set the default values.
    • A Boolean indicating whether the default values should is set more than once.

      When false , the system sets the default values is only if this method had never been called in the past (or the the KEY_HAS_SET_DEFAULT_VALUES D Efault value shared Preferences file is False).

As long as you set the "third argument false to," can safely call this method every time your activity starts without OV Erriding the user ' s saved preferences by resetting them to the defaults. However, if you set it true to, you'll override any previous values with the defaults.

Android Preferences (4) Set default values

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.