U Avalos
U Avalos

Reputation: 6798

Correct way of detecting preference changes in a live wallpaper?

I created a live wallpaper with a preference activity. Unfortunately, any time the user changes a setting the onSharedPreferenceChangeListener gets called. The listener calls some routines that are somewhat CPU intensive (reinitialize a large mesh) so it makes the preference activity sluggish. How can I call the listener only when the user exits the preference activity?

Upvotes: 0

Views: 1396

Answers (2)

berlindev
berlindev

Reputation: 1751

Maybe you should use onPause or onDestroy in your preferenceActivity to calculate your large mesh. Seems you just want to recalculate on changes on any attribute, so just set a flag in onPreferencesChanged and do your maths when activity is closed and your going back to liveWallpaper-View.

Another way would be to start your calculation in a parallel thread, to let the preferenceActivity act as normal.

Upvotes: 1

slayton
slayton

Reputation: 20319

Get an instance of a SharedPreferences object with:

SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);

Then register a SharedPreferences.OnSharedPreferenceChangeListener

I usually have my Engine class implement the SharedPreferences.OnSharedPreferenceChangeListener interface so I simply invoke inside the constructor of my Engine:

mPrefs.registerOnSharedPreferenceChangeListener(this);

Then simply implement the onSharedPreferenceChanged(SharedPreferences prefs, String key) method in your Engine class.

You could also implement a separate class to act as the OnSharedPreferenceChangeListener if you like.

Upvotes: 2

Related Questions