I have legacy code that extends PreferenceActivity with a subclass called Preferences. The PreferenceActivity function is called as follows:
Intent intent = new Intent(this, Preferences.class); this.startActivity(intent);
OnSharedPreferenceChangeListener exists in another fragment (and not in a subclass of PreferenceActivity), but reference to PreferenceActivity requires a link to custom preference / control attributes similar to the following:
pref = (CheckBoxPreference) prefActivity.findPreference(res.getString(R.string.keyAccount)); pref.setSummary("something");
where "prefActivity" is a reference to PreferenceActivity. Can anyone suggest how to keep the reference to the PreferenceActivity when it is created, or otherwise find the PreferenceActivity when necessary?
EDIT: Including highly simplified code, hopefully helps show hierarchies and clarify.
FragmentActivity CPActivity creates an instance of CPFragment and, on demand (button click), creates an intent to disable the PreferenceActivity subclass (called "Preferences").
public class CPActivity extends FragmentActivity { public static CPActivity inst; private CPFragment mFragmentCP; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); inst = this; mFragmentCP = new CPFragment(); } public void onSettingsButtonPressed() {
CPFragment is our common preference listener (by the way). It is in this code that we would like to change the user control / preference record (that is, not the preference value itself, but rather the attributes in the preference control, for example, CheckBoxPreference). We would like to do this here, because it is here that the corresponding data is calculated.
public class CPFragment extends Fragment implements OnSharedPreferenceChangeListener { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
And finally, the PreferencesActivity subclass of Settings does a bit more than the Settings view displays.
public class Preferences extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences_cp); } }
As I mentioned, we would prefer to be able to change user preference in CPFragment (as opposed to PreferenceActivity). So I was looking for a way to find PreferenceActivity by responding as onSharedPreferenceChangeListener in CPFragment.