If I understand your question correctly, you have an Activity named Preferences which extends PreferenceActivity. You also have a Fragment that has registered an OnSharedPreferenceChangeListener. You need to update the UI in your Preferences Activity but you are not sure how to accomplish this.
Is the Fragment attached to the Preferences Activity? If it is, then you should be able to do the following in your Fragment:
if (getActivity() instanceof Preferences) {
Preferences activity = (Preferences) getActivity();
CheckBoxPreference pref = (CheckBoxPreference) activity.findPreference(getString(R.string.keyAccount));
pref.setSummary("something");
}
Another approach:
You can also register an OnSharedPreferenceChangeListener in your Preferences Activity and you will get notified when the preference changes. Example:
public class Preferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
/* ... */
@Override protected void onStart() {
super.onStart();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(getString(R.string.keyAccount))) {
CheckBoxPreference pref = (CheckBoxPreference) findPreference(key);
pref.setSummary("something");
}
}
@Override protected void onPause() {
super.onPause();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
/* ... */
}
Some things to consider based on your edited answer:
1) Never create a static reference to your Activity. public static CPActivity inst;. This can lead to memory leaks.
2) Consider moving code in your Preferences Activity to a PreferenceFragment.
3) It is still unclear what you are trying to achieve. Is the CheckBoxPreference that you want to modify in your CPFragment or the Preferences Activity?
4) You should still consider using two OnSharedPreferenceChangeListeners or an EventBus.