I've (literally) lost the whole weekend debugging this issue. I
finally found what is causing it and I'm quite confident it's a bug
(or a very-very undocumented feature). I apologize for not posting any
code, it's on my other computer; I can later post a simplified repro
case if necessary.

Basically what I'm doing is a very basic preference screen to/from
which I want to add/remove preferences after it has been created.
Doing this type of changes on the activity's onCreate() method works
just fine. However, if I call startActivityForResult() and (in
onActivityResult()) call addPreference() nothing is added to the view
even though addPreference() returns true and the value returned by
getPreferenceCount() is incremented by 1. In addition to nothing else
showing up on the screen another issue is that the value returned by
getRootAdapter().getCount() remains the same, meaning that the adapter
doesn't get notified of the addition.

What happens, I think (because it's really difficult to debug with
sources that don't match the SDK release), is that the
PreferenceScreen instance's mListener field (which should be an
android.preference.PreferenceGroupAdapter instance) is actually null
so it doesn't get notified of the addition. The problem comes from
PreferenceGroupAdapter:139 (
http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/preference/PreferenceGroupAdapter.java;h=05c2952243746f9a1b8a56d72f2a0d8be563d7bf;hb=HEAD#l139
) where the adapter registers as a listener to change events on all
children of the PreferenceScreen but not on the PreferenceScreen
itself. Meaning that any changes to the PreferenceScreen don't result
in any notifications to the adapter.

I've been checking the Settings app (which does something similar in
e.g. WifiSettings -- dynamically adding and removing WiFi networks)
and couldn't understand why that worked and my code didn't. The
answer, it seems, is that all the activities in the Settings app add/
remove preferences to/from a PreferenceCategory that's already added
to the PreferenceScreen in onCreate(). So the PreferenceCategory
already has the adapter registered as
OnPreferenceChangeInternalListener and any changes to it correctly get
reflected on the screen.

If everyone is too busy to fix this I have no objections to coming up
with a patch myself. The code change should be minimal -- it just
needs to be done correctly, which makes it a tad more difficult.

Cheers,
Alin.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Android Discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/android-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to