registerOnSharedPreferenceChangeListener not working

Viewed 1170

I'm trying to update my app background color by changing the preferences but the method onSharedPreferenceChanged is never reached. The preferences are changed successfully, but I the listener doesn't work properly:

MainActivity:

public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

private RecyclerView mRecyclerView;
private ContactsAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(myToolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

@Override
public void onResume() {
    super.onResume();

    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    mSettings.registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onPause() {
    super.onPause();

    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    mSettings.unregisterOnSharedPreferenceChangeListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu){
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_settings:
            Intent intent = new Intent(this, MyPreferenceActivity.class);
            startActivity(intent);
            return true;
        case R.id.action_favorite:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    if(key.equals("color")) {
        String color = sharedPreferences.getString("color", "3");

        int colorId = Integer.valueOf(color);

        // Alterar background

        ViewGroup viewGroup = (ViewGroup) ((ViewGroup) (findViewById(android.R.id.content))).getChildAt(0);

        if (colorId == 1) {
            viewGroup.setBackgroundColor(Color.WHITE);
        } else if (colorId == 2) {
            viewGroup.setBackgroundColor(Color.YELLOW);
        } else if (colorId == 3) {
            viewGroup.setBackgroundColor(Color.RED);
        }

    }
 }
}

MyPreferenceActivity:

public class MyPreferenceActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preference);

}
}

The method onSharedPreferenceChanged is never called, the only way I got it to work was implement the method OnSharedPreferenceChangeListener directly inside the onResume, but sometimes it works fine, sometimes it doesn't and the method is not reached.

2 Answers

The Shared preference registerOnSharedPreferenceChangeListener is valid only when MainActivity is in foreground else that will be unregistered in onPause.

When you are creating a PreferenceActivity named MyPreferenceActivity, the onPause() of MainActivity is called, where the listener is getting unregistered.

One work around could be, unregister from the listener onStop() instead onPause() in MainActivity.

MainActivity.java

@Override
public void onStart() {
    super.onStart();

    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(this);
    mSettings.registerOnSharedPreferenceChangeListener(this);
}

@Override
protected void onStop() {
    super.onStop();

    SharedPreferences mSettings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    mSettings.unregisterOnSharedPreferenceChangeListener(this);

}
Related