Well, I tested one of the sample applications (http://code.google.com/
p/androgames-sample/) and both the Engine's and Settings'
onSharedPreferenceChanged() methods are called and with the same
instance of SharedPreferences object.  So, apparently something is
different in my Live Wallpaper.

I went through everything top to bottom and made some small (I'd say
very minor) tweaks to my code/config to further match the example and
still the same results - the Engine's onSharedPreferenceChanged()
method is not called while the Settings' is.

I also noticed that SharedPreferences instance returned in the Engine
constructor (via prefs =
LiveWallpaperService.this.getSharedPreferences(PREFS_NAME,
0)) is a different object instance than that passed in to
onSharedPreferenceChanged() of the Settings class.  Nowhere do I
create a new instance of SharedPreferences nor do I have any other
code related to shared preferences outside of what is being discussed
here.  So, Android is serving up 2 separate instances of
SharedPreferences for some reason.... Why?

I am boggled.  Please help me think.

On Apr 16, 11:45 am, shaun <[email protected]> wrote:
> I am experiencing something strange with a Live Wallpaper.  I very
> closely followed several examples out there on Live Wallpapers in
> regards to shared preference settings (e.g. Android sample app Cube
> Live Wallpaper).
>
> The Engine implements
> SharedPreferences.OnSharedPreferenceChangeListener and the pertienent
> code in Engine looks like this:
>
> public static final String PREFS_NAME = "some-name";
>
> Engine() {
>     prefs = LiveWallpaperService.this.getSharedPreferences(PREFS_NAME,
> 0);
>     prefs.registerOnSharedPreferenceChangeListener(this);
>
> }
>
> public void onSharedPreferenceChanged(SharedPreferences
> sharedPreferences, String key) {
>           Log.e(getClass().getSimpleName(), "on shared preference
> changed");
>
> }
>
> NOTE: I never unregister the Engine instance as a shared prefs
> listener.  Although, I did move some things around to onCreate and
> onDestroy (added the unregister there) and had no impact as far as I
> could tell.
>
> The Settings class extends PreferenceActivity and implements
> SharedPreferences.OnSharedPreferenceChangeListener as well.  And since
> that class has little code here is the body:
>
>     @Override
>      protected void onCreate(Bundle icicle) {
>          super.onCreate(icicle);
>
> getPreferenceManager().setSharedPreferencesName(Engine.PREFS_NAME);
>          addPreferencesFromResource(R.xml.settings);
>
> getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceCha­ngeListener(this);
>      }
>
>     �...@override
>      protected void onDestroy() {
>
> getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceC­hangeListener(this);
>          super.onDestroy();
>      }
>
>      public void onSharedPreferenceChanged(SharedPreferences
> sharedPreferences, String key) {
>           Log.e(getClass().getSimpleName(), "on shared preference
> changed");
>      }
>
> The only thing coming out in the log is from the Settings class for on
> shared pref change.  Anyone have any idea why?
>
> In the implementations I've found for Live Wallpaper shared prefs
> settings, the Engine constructor either uses the shared preferences or
> calls to onSharedPreferenceChanged() directly.  It seems like those
> other examples may be experiencing the same thing I described above,
> and they worked around it!?!?
>
> I do admit I have not taken the example code from others and tried it
> out to see if the Engine onSharedPreferenceChanged() gets called.  I
> will do that tonight or this weekend.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" 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 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" 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-developers?hl=en

Reply via email to