Skip to main content

Android Preferences: How to load the default values when the user hasn"t used the preferences-screen?



I am using a PreferenceActivity to let the user set some values. I am feeding it the xml file with the defined preferences.





I have set all the android:defaultValue="" for them.





When I start my application, I need the preferences, or if they are not set yet manually, I want the default values:







SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

boolean value = prefs.getBoolean("key"), false);







However, when android:defaultValue="true" I still get false . So, it looks like the defaultValues set in the XML are not used anywhere but when initializing the preferences-screen.





I don't want to hardcode the default values in the getBoolean() method. So, is there a way get the default-values with only defining these in 1 place?


Comments

  1. this question is similar to mine:

    initialize-preferences-from-xml-in-main-activity

    Just use this code in onCreate method:

    PreferenceManager.setDefaultValues(this, R.xml.preference, false);


    It will load your preferences from XML, and last parameter (readAgain) will guarantee that user preferences won't be overwritten.

    Take a look into PreferenceManager.setDefaultValues in Android API for further investigation.

    ReplyDelete
  2. Be aware that if you are using getSharedPreferences(String sharedPreferencesName, int sharedPreferencesMode) to retrieve preferences you have to use PreferenceManager.setDefaultValues(Context context, String sharedPreferencesName, int sharedPreferencesMode, int resId, boolean readAgain) to set defaults! For example:

    PreferenceManager.setDefaultValues(this, PREFS_NAME, Context.MODE_PRIVATE, R.xml.preference, false);


    I hope this can help someone.

    ReplyDelete
  3. For example extending DialogPreference I do this:

    @Override
    protected void onSetInitialValue(boolean restore, Object defaultValue) {
    super.onSetInitialValue(restore, defaultValue);

    if (restore) {
    mValue = shouldPersist() ? getPersistedString(mDefault) : mDefault;
    } else {
    mValue = mDefault;
    }
    }


    mDefault can be:


    mContext.getResources().getString(attrs.getAttributeResourceValue(androidns,"defaultValue", 100));
    something you have indexed in R.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...