I had the same problem and fixed it by persisting the value in the 
onSetInitialValue method.  The javadoc says: "If restorePersistedValue is 
false, you should set the Preference value to defaultValue that is given 
(and possibly store to SharedPreferences if 
shouldPersist()<http://developer.android.com/reference/android/preference/Preference.html#shouldPersist%28%29>is
 true)."  It's that second part that is required.

On Friday, September 21, 2012 11:50:28 AM UTC-5, Gene Wildhart wrote:
>
> Your solution works perfectly for me on Gingerbread (Android 2.3.7) and 
> below.  However, when trying on ICS (4.0+) or newer, neither the *
> onSetInitialValue* or the *onGetDefaultValue* functions are ever called. 
>  Can anyone explain why the behavior changed in ICS?
>
> On Friday, July 27, 2012 1:41:50 AM UTC-7, Jens wrote:
>>
>> Finally, I've found a solution to this problem!
>>
>> The official documentation for the preferences activity has just been 
>> updated.
>> Check out the paragraph about "Building a Custom Preference"
>> http://developer.android.com/guide/topics/ui/settings.html#Custom
>>
>> You have to implemenet both of the following methods the way it is 
>> described.
>> Now *PreferenceManager.setDefaultValues()* also works for my custom 
>> dialog!
>>
>> protected void onSetInitialValue(boolean restorePersistedValue, Object 
>> defaultValue)
>>
>> protected Object onGetDefaultValue(TypedArray a, int index)
>>
>>
>>
>> On Sunday, July 15, 2012 2:18:37 PM UTC+2, jdr88 wrote:
>>>
>>> I just came across the same issue and can confirm the same behaviour:
>>>
>>> *android:defaultValue* of a custom DialogPreference is *not* set
>>> with PreferenceManager.setDefaultValues() method.
>>>
>>> Seems like a bug?
>>> I'm using now the solution that you proposed...
>>>
>>>
>>> On Thursday, 13 October 2011 23:11:27 UTC+2, tlegras wrote:
>>>>
>>>> Okay, I give up. 
>>>> My preference default value will be copied in the preference access 
>>>> code as well:
>>>>
>>>> value = sharedPref.getInt(mykey,myDirtyHardcodedDefaultValue );
>>>>
>>>>
>>>> If ever someone has a better solution, it will be welcome :)
>>>>
>>>> Thierry.
>>>>
>>>>
>>>>  2011/10/11 Thierry Legras <tle...@gmail.com>
>>>>
>>>>> Hi Ibendlin,
>>>>>
>>>>> Thanks for helping me :)
>>>>>
>>>>> I don't think the problem is in SeekBarPreference.onSetInitialValue() 
>>>>> function itself as it is never called (I have put a log in it to check). 
>>>>> There is certainly an issue somewhere in my implementation; when 
>>>>> setDefaultValues is called, only SeekBarPreference constructor is called. 
>>>>>
>>>>> I was suspecting the xml, but I don't see any problem in it 
>>>>> (android:defaultValue is set and android:persistent as well):
>>>>>
>>>>>     <com.tlegras.tools.SeekBarPreference 
>>>>>         android:key="preferences_epg_daytostore"
>>>>>         android:title="@string/preferences_epg_daytostore_title"
>>>>>         android:summary="@string/preferences_epg_daytostore_summary"
>>>>>         android:dialogMessage="Nombre de journées à télécharger. 
>>>>> Attention, plus ce nombre est élevé, plus le chargement sera long et 
>>>>> moins l'application sera réactive."
>>>>>         android:text=" jour(s)"
>>>>>         android:defaultValue="4"
>>>>>         android:max="5"
>>>>>         android:persistent="true" 
>>>>>         />
>>>>>
>>>>>
>>>>> 2011/10/11 lbendlin <lu...@bendlin.us>
>>>>>
>>>>>> it took me a few weeks to get that right.  here's my example for a 
>>>>>> boolean value. And yes, .setDefaultValues is actually working, despite 
>>>>>> what 
>>>>>> you may think :-/
>>>>>>
>>>>>>    @Override
>>>>>>     protected void onSetInitialValue(boolean restoreValue, Object 
>>>>>> defaultValue) {
>>>>>>     boolean temp = restoreValue ? getPersistedBoolean(false) : 
>>>>>> defaultValue.toString().equals("true") ? true : false;
>>>>>>     if (!restoreValue)
>>>>>>         persistBoolean(temp);
>>>>>>     this.oldValue = temp;
>>>>>>     }
>>>>>>
>>>>>>  -- 
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Android Developers" group.
>>>>>> To post to this group, send email to android-d...@googlegroups.com
>>>>>> To unsubscribe from this group, send email to
>>>>>> android-developers+unsubscr...@googlegroups.com
>>>>>> For more options, visit this group at
>>>>>> http://groups.google.com/group/android-developers?hl=en
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> Thierry.
>>>>>
>>>>
>>>>
>>>>
>>>> -- 
>>>> Thierry.
>>>>  
>>>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to