Android: how do I call findPreference () from my main activity?
I am handling the settings screen of my android app. I want to disable (exclude gray) an element if the previous one has a specific value.
I have implemented two classes, MainActivity and PreferencesActivity.
In MainActivity I do:
public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
...
public void onCreate (Bundle savedInstanceState) {
...
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
sharedPrefs.registerOnSharedPreferenceChangeListener(this);
...
}
Then when my preference is requested by the user:
startActivity(new Intent(this, PreferencesActivity.class));
Then, to handle the settings in MainActivity, I do:
@Override
public void onSharedPreferenceChanged (SharedPreferences prefs, String key) {
if ("sport".equals(key)) {
sport = prefs.getString("sport", "");
ListPreference lp = findPreference("match_duration");
if (sport.equals(getString(R.string."sport_soccer"))) {
lp.setEnabled(false);
}
}
...
}
The problem is that I cannot call findPreference in MainActivity ("The findPreference (String) method is undefined for type MainActivity" ...). Is my approach wrong? Shoud Am I implementing onSharedPreferenceChanged () method in PreferencesActivity? If so, how do I use the MainActivity properties in the PreferencesActivity?
+3
source to share