How to refer or "find" a preference function?

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() { // Bring up the Preferences menu Intent intent = new Intent(this, Preferences.class); this.startActivity(intent); } } 

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) { // In response to preference changes, we want to modify the PreferenceActivity controls. // So it seems we would need a reference to the PreferenceActivity subclass "Preferences } } 

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.

+6
source share
1 answer

If I understand your question correctly, you have an Activity named Preferences , which extends PreferenceActivity . You also have a Fragment that registered OnSharedPreferenceChangeListener . You need to update the user interface in Preferences Activity , but you do not know how to do this.

Is Fragment connected to Preferences Activity ? If so, you should be able to do the following in 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 OnSharedPreferenceChangeListener in your Preferences Activity , and you will receive a notification when the preference is changed. 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 link to your Activity . public static CPActivity inst; . This may cause a memory leak.

2) Consider moving code in Preferences Activity to PreferenceFragment . p>

3) It is not yet clear what you are trying to achieve. Is the CheckBoxPreference you want to change in CPFragment or Preferences Activity ?

4) . You should consider using two OnSharedPreferenceChangeListener or EventBus .

+4
source

All Articles