Showing posts with label SharedPreferences. Show all posts
Showing posts with label SharedPreferences. Show all posts

Sunday, April 20, 2014

RingtonePreference and SharedPreferences.OnSharedPreferenceChangeListener

Revision

This problem was actually something different than what I (and stackoverflow) originally thought. RingtonePreference notifies listeners just fine—it has to, because the notification happens at the level of the SharedPreference file, and there's nothing client code can do to shut that behavior off. What was actually happening was this: RingtonePreference launches a new activity that partially conceals the PreferenceActivity. This causes the PreferenceActivity to be paused. And I was unregistering my listener in onPause(). I switched the (un)registration to onStart()/onStop() instead, and everything worked fine.

Original Post

Since I seem to have gotten into the habit of documenting Android framework bugs and surprises as I encounter them, here's another. RingtonePreference doesn't notify the underlying SharedPreference file's OnSharedPreferenceChangeListeners when it persists its changes. A workaround is to register a Preference.OnPreferenceChangeListener on each individual RingtonePreference instead. Just remember that this handler is notified before the change gets persisted to storage, so if you need to use the new value in the handler, get it from the Object that is passed in rather than from the SharedPreferences file itself.

Also remember that both OnPreferenceChangeListener and OnSharedPreferenceChangeListener are notified before any dependency-related updates are applied, so for a Preference whose enabled state is linked to a different Preference, you can't rely on isEnabled() returning the correct value inside either type of handler.

Wednesday, April 16, 2014

Listening to Preferences

We'll keep this one quick. Before you spend a frustrating evening trying to figure out why sometimes your SharedPreferences.OnSharedPreferenceChangeListener is called and sometimes it isn't, and before you take up astrology due to a growing conviction that it is somehow related to the alignment of the stars, take note of the fact that SharedPreferences stores its listeners internally as weak references (with good reason; see addendum). No, they don't bother to document this fact. So if you do this:

PreferenceManager.getDefaultSharedPreferences(this)
    .registerOnSharedPreferenceChangeListener(
        new SharedPreferences.OnSharedPreferenceChangeListener()
        {
            @Override public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key)
            {
                // do important things
            }
        });

your anonymous listener immediately becomes a candidate for garbage collection. But you can't predict exactly when that will happen. Sometimes it will work (for a while), sometimes it won't. Do this instead:

mListener = new SharedPreferences.OnSharedPreferenceChangeListener()
{
    @Override public void onSharedPreferenceChanged(
        SharedPreferences sharedPreferences, String key)
    {
        // do important things
    }
};

PreferenceManager.getDefaultSharedPreferences(this)
    .registerOnSharedPreferenceChangeListener(mListener);

It's one more obnoxious member variable to keep around (or interface for your class to implement if you choose that route), but it's what you've gotta do.

Addendum


I think it actually makes good sense to use weak registration for observers, and that is in fact how I have typically implemented things in my own frameworks (in a perfect world, observers could choose whether they wanted to register strongly or weakly). The reason is that strong registration can easily lead to memory leaks when the observed is a persistent object and the observers are (meant to be!) transient. If SharedPreferences did in fact use strong observer registration and I did something like the first code snippet in an Activity, it would be a big problem--since the anonymous observer has an implicit reference to its outer class and there is no way to unregister it, the Activity object would be leaked and kept alive until whenever the SharedPreferences was destroyed. In my case the outer class is the Application object and I want that observer to stick around for as long as my app is running, but I imagine it's concerns like these that led to the choice of weak registration in the first place.

So: a sound decision, but since it differs from the decision made in (so far as I know) all other cases, they should have documented it. :)