Android CheckBoxPreference Default Value

I have the following XML code for my CheckBoxPreference:

<CheckBoxPreference
    android:key="pref_boot_startup"
    android:title="Auto start"
    android:defaultValue="true" />

But when I retrieve the preference in code the value is false.

  • Class not found error, but class definitely exists
  • Text clipping inside ListPopupWindow on Android
  • How to persist permission in android API 19 (KitKat)?
  • BaseAdapter or ArrayAdaptor - Android
  • Issue with cancelling the AlarmManager - PendingIntent
  • How to create application specific folder in android gallery?
  • sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean autoStart = sharedPreferences.getBoolean("pref_boot_startup", true);
    

    My autoStart variable returns false.

    Is there a specific reason for this? Am I missing a step to set the default value to true?

    Related posts:

    Should use android:showAsAction when not using the appcompat library
    ListView inside a ScrollView
    Target host must not be null or set in parameters.scheme=null,host=null
    iOS SegmentedControl equivalent in Android
    Only some users reporting &ldquo;Resource Not Found&rdquo; error. Does this make sense?
    Set button text to question mark
  • Android fitsSystemWindows not working when replacing fragments
  • Warning: This <FrameLayout> can be replaced with a <merge> tag
  • Android: How to add R.raw to project?
  • Android: How to get installer file name programatically?
  • Android Intent.getStringExtra() returns null
  • color value drawable resource issue
  • 3 Solutions collect form web for “Android CheckBoxPreference Default Value”

    You have to set the defaults first:

        @Override
        protected void onCreate()
        {
            PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            boolean autoStart = sharedPreferences.getBoolean("pref_boot_startup", true);
    
         {...}
        }
    

    Use junkdog’s method, but for what it’s worth, this is a bug in Android:

    http://code.google.com/p/android/issues/detail?id=6641

        // These two lines are working around an android bug:
        // http://code.google.com/p/android/issues/detail?id=6641
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        prefs.edit().putBoolean(REFRESH_COUNTER_PREF, prefs.getBoolean(REFRESH_COUNTER_PREF, true)).commit();
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.