Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSharedPreferenceChanged() Never called

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.

like image 472
Jon Avatar asked Jun 08 '26 11:06

Jon


1 Answers

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);
}
like image 76
Fred Ondieki Avatar answered Jun 10 '26 02:06

Fred Ondieki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!