Rajawali / Rajawali

Android OpenGL ES 2.0/3.0 Engine
https://rajawali.github.io/Rajawali/
Other
2.35k stars 700 forks source link

Using SharedPreferences with Live Wallpapers #532

Closed ToxicBakery closed 11 years ago

ToxicBakery commented 11 years ago

Feel free to add and improve on this page. Actually, if anyone has anything useful to add to the wiki please feel free to email me changes and I will do my best to get them added.

https://github.com/MasDennis/Rajawali/wiki/Using--SharedPreferences-with-Live-Wallpapers

Davhed commented 11 years ago

Ian, thank you for putting this together. It clarifies the concept completely... I think...

Basically as I understand it you are creating a data set from the preferences. So every change that is made by the user is immediately reflected in that data by setting, and is accessible from all relative classes by getting.

This is different from the listener/handler approach because it is not relying on events, but instead by manual polling.

Please let me know if I misunderstood, but I am going to try this out on my next project.

ToxicBakery commented 11 years ago

Yep. You could avoid the static class and it 'should' work but I have found this to be more convenient. The main point to drive home is the listeners. They do not work as you might expect for various reasons so it is best to avoid them.

sandrotra commented 11 years ago

Ehi mate, it works fine! Thanks for existing, I love you! ;-)

jayschwa commented 11 years ago

Does the official Android documentation provide any good examples in this area?

It might be nice to eventually roll this into the WallpaperTemplate. Even if it has just a single option for picking the box color or something, that might help newbies quite a bit. I know I was confused as hell the first time I started playing with Preferences.

ToxicBakery commented 11 years ago

Well I think the class level comment for SharedPreferences highlights the problem. The description of the class makes you think a singleton exists for each preference file but this is clearly not the case as an Activity and a Service can be given different instances of the SharedPreferences class causing listeners to never be called.

"Note: currently this class does not support use across multiple processes. This will be added later."

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/content/SharedPreferences.java#SharedPreferences

AndrewJo commented 11 years ago

I'm guessing it's fine if you're only going to read in the preference values in the beginning. But what about a case where you need to reflect change right away when a setting has been changed?

I implement my shared preference listener in my WallpaperEngine class (which is how Android LWP sample project does it) and whenever a preference has been changed, it fires up a public method in my Renderer class to reflect that change in my scene.

ToxicBakery commented 11 years ago

I have accomplished this in the past with weak references to handlers set by the relevant activities and services. Seems to work just fine =)