Reputation: 136
I am using a switch statement to check the origin of the preference.
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Preference pref = findPreference(key);
switch(key) {
case "key_video_quality":
Log.i("Tag1", "Video quality button pressed");
break;
case "key_video_length":
Log.i("Tag2", "Video length button pressed");
break;
case "key_auto_save":
Log.i("Tag3", "Auto save button pressed");
break;
case "key_dark_mode":
Log.i("Tag4", "Dark mode button pressed");
break;
}
}
}
key_auto_save
is never triggered, and key_dark_mode
is, they are both SwitchPreferences
.
Is there a method to check the current value of switch preference when it is pressed so i can decide to switch on/off a dark theme for my application? As i am using it as a general Preference
i don't think isChecked()
applies here.
Also, i'm unsure on the key_auto_save
problem, the key is correct.
Upvotes: 1
Views: 3271
Reputation: 136
in my onSharedPreferenceChanged
method, in case "key_dark_mode":
i used sharedPreferences.getBoolean(key, false);
to return if my switchPreference
was true or false.
Upvotes: 1
Reputation: 655
Are you registering the listener onResume
?
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
It is hard to judge without seeing more code, for example are you extending PreferenceActivity
?
Upvotes: 0