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);


Then when my preference is requested by the user:

startActivity(new Intent(this, PreferencesActivity.class));


Then, to handle the settings in MainActivity, I do:

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"))) {


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?


source to share

1 answer


should be called from the class that implements the interface PreferenceActivity

( PreferencesActivity

in my context). Properties MainActivity

can be accessed through the class SharedPreferences




All Articles