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:
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