I've got a custom Preference
in my app that uses a SoundPool
(it's a volume control slider that plays a sample when the slider position is change.)
I'd like to call unload()
on the SoundPool when my Preference stops being shown to clean up after myself. Is there a good place to do that? In an Activity
I'd put it in onDestroy()
.
I took a guess at onPrepareForRemoval()
but that doesn't seem to be called.
In the end, I couldn't find a reliable place to clear down resources in a Preference. The approach I took instead was to use a custom PreferenceActivity, and manage the SoundPool resource for its child Preferences in there. So, create the SoundPool in the PreferenceActivity's onCreate(), and close it up in onDestroy(), as you would for a SoundPool used in any activity.
(My child Preferences grabbed the SoundPool by simply casting their Context object to my custom PreferenceActivity and using a getter. I know it's a bit unpleasant to manually cast a Context down like that, but I can't see how there could be any place where this custom Preference's context won't be my custom PreferenceActivity...)