3

I recently imported the HoloEverywhere library from GitHub into Eclpse and have begun using it in an app that has already been working. Overall I am happy with the library and things have been going well.

I am trying to use the org.holoeverywhere.preference.PreferenceActivity in place of my old PreferenceActivity. The UI looks as it should, but I noticed that onSharedPreferenceChanged() never gets called anymore. What am I doing wrong?

AFAIK I am using the library as intended. I have barely changed anything from my old version to the new version using HoloEverywhere. While there are many related questions on SO, I could not find anything that addresses my problem.

Relevant code posted below:

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.preference.Preference;
import org.holoeverywhere.preference.PreferenceFragment;
import org.holoeverywhere.preference.PreferenceManager;
import org.holoeverywhere.preference.PreferenceScreen;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.Editor;
import org.holoeverywhere.preference.SharedPreferences.OnSharedPreferenceChangeListener;


public class SettingsActivity extends org.holoeverywhere.preference.PreferenceActivity implements SyncManager.SyncProgressListener, SharedPreferences.OnSharedPreferenceChangeListener
{
private static SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState)
    {
    prefs = PreferenceManager.getDefaultSharedPreferences( this );
    }

public void onSharedPreferenceChanged(SharedPreferences prefs, String key)
    {
    // do some really important stuff here
    }

public static class DisplaySetttingsFragment extends PreferenceFragment
    {
    @Override
    public void onCreate(Bundle savedInstanceState)
        {
        super.onCreate( savedInstanceState );
        getActivity().setTitle( getString( R.string.pref_display_title ) );
        addPreferencesFromResource( R.xml.display_preferences );
        }

    @Override
    public void onResume()
        {
        super.onResume();
        prefs.registerOnSharedPreferenceChangeListener( (SettingsActivity) getActivity() );
        }

    @Override
    public void onPause()
        {
        super.onPause();
        prefs.unregisterOnSharedPreferenceChangeListener( (SettingsActivity) getActivity() );
        }
    }
}

Update: An example where I change and commit preferences and I would expect onSharedPreferenceChanged() to be called

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
        {

        String resetString = getString( R.string.pref_key_reset_display );
        String key = preference.getKey();

        if ( key != null && key.equals( resetString ) )
            {
            prefs.edit().
                putBoolean( getString( R.string.pref_key_reset_display ), true ).commit();
            }

        return super.onPreferenceTreeClick( preferenceScreen, preference );
        }

Update: I do not believe this is a problem of my preferences not having a registered listener (in this case my SettingsActivity) at the time of a commit() or apply(). I am able to debug and see that the WeakHashMap inside prefs for listeners always has my activity as a member. I have tried creating an global variable that is a listener, but it makes no difference.

Jon
  • 1,820
  • 2
  • 19
  • 43

4 Answers4

6

The might be missing the below calls: this might solve your problem.

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

/* (non-Javadoc)
 * @see android.app.Activity#onPause()
 */
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    getPreferenceScreen().getSharedPreferences()
    .unregisterOnSharedPreferenceChangeListener(this);
}
Fred Ondieki
  • 2,314
  • 25
  • 23
2

try to call

prefs.registerOnSharedPreferenceChangeListener(this)

in onCreate in SettingsActivity. Calling it in Fragment, in inner class, will destroy the listeners when inner fragment is Paused, I suppose

Jacek Milewski
  • 3,304
  • 1
  • 18
  • 17
  • No difference. When debugging I am able to see `prefs`' inner members. It has a `WeakHashMap` for storing the listeners, but I always see my Activity as a member regardless of where I register/unregister the listener. – Jon Jul 01 '13 at 20:22
0

Check the preference.xml. All the preferences have a defined key value? If not the callback will not be fired.

0

I had a similar problem. For me the listener was called for a standard property but nor for one set with the editor.

The solution came from the doku for the listener

This may be called even if a preference is set to its existing value.

The solution for me was setting the boolean first to the wrong and then to the correct value(and committing each change).

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41