When I check if the checkbox is checked, I get a Force Close in the following code:
public class preference extends PreferenceActivity implements OnSharedPreferenceChangeListener {
public static final String nwd = "nwd";
private CheckBoxPreference nwd_pref;
#Override
public void onCreate(Bundle savedInstanceState) {
...
nwd_pref = (CheckBoxPreference)getPreferenceScreen().findPreference(nwd);
...
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
...
if (nwd_pref.isChecked()){
// do code
}
...
}
}
I found this example during searching but looks the same to me.
Thanks for your help!
findPrefernece(..) will "Return The Preference with the key, or null." It looks like "nwd" hasn't been set yet.
Related
What I'm doing is this :
class Act extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
After that override one method
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
}
I'm changing my keys in service class using SharedPreferences I want to get notified as soon as preferences in Service class gets changed.
Thank you.
You must also register and unregister the listener with the Shared Preference object you are using.
#Override
public void onCreate() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onDestroy() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
I have an Example class that implements OnSharedPreferenceChangeListener.
public class Example implements SharedPreferences.OnSharedPreferenceChangeListener {
public Example(Context cont) {
this.context = cont;
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(cont);
sharedPref.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Log.d("TAG", "A preference has changed >>" + key);
}
}
However, the onSharedPreferenceChanged method never gets called when I change a preference in the SettingsActivity hosting a fragment:
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
Why?
Okay I started to implement this horrible code from Google's android. The OnSharedPreferenceChangeListener is not being called. This is my code, can you please advice?
Class definition:
private SharedPreferences sPrefs;
private PreferenceChangedListener prefsChangedListener;
I have a private inner class:
private class PreferenceChangedListener implements
OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
if (key == "highThreshold") {
try {
highThreshold = Float.parseFloat(sharedPreferences
.getString(key, "0"));
} catch (Exception e) {
}
}
}
}
Tried te following code in OnResume and to register the listener after the 'Voorkeuren' preferenceactivity is started. Both fail.
sPrefs = getPreferences(MODE_PRIVATE);
prefsChangedListener = new PreferenceChangedListener();
sPrefs.registerOnSharedPreferenceChangeListener(prefsChangedListener );
I defined a class Voorkeuren which extends PreferenceActivity
public class Voorkeuren extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_voorkeuren, menu);
return true;
}
}
Which I open as following:
Intent intent = new Intent(getBaseContext(),Voorkeuren.class);
startActivity(intent);
The class shows up fine, and stores the values between sessions. But my application should not be polling if settings chagned. Any ideas? I have red something about an SharedPreferences.Editor but I am not sure how it is related.
Not sure what your question is and what is desired.
But there is one change that you need to do. Replace this in onResume
sPrefs = getPreferences(MODE_PRIVATE);
prefsChangedListener = new PreferenceChangedListener();
sPrefs.registerOnSharedPreferenceChangeListener(prefsChangedListener );
to
sPrefs = PreferenceManager.getDefaultSharedPreferences(this);;
prefsChangedListener = new PreferenceChangedListener();
sPrefs.registerOnSharedPreferenceChangeListener(prefsChangedListener);
in onCreate()
i wrote a simple PreferenceScreen, looks as follows:
public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.setOnPreferenceChangeListener(this);
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getTitle().equals(getString(R.string.settings_diverse_about))) {
// TODO open about dialog
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
private static String PATTERN_EMAIL = "[A-Z0-9._%-]+#[A-Z0-9.-]+\\.[A-Z]{2,4}";
private static String PATTERN_TWITTER = "[a-zA-Z0-9_-]+";
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String prefTitle = preference.getTitle().toString();
// phone will be fully handled by inputType
String newStr = newValue.toString();
if (prefTitle.equals(getString(R.string.settings_contact_email))) {
return newStr.trim().matches(PATTERN_EMAIL);
} else if (prefTitle.equals(getString(R.string.settings_contact_twitter))) {
return newStr.trim().matches(PATTERN_TWITTER);
}
return true;
}
The problem is, the onPreferenceChange method gets never invoked.
Furthermore, what do you think about the way of validating the attributes?
I think it's not really optimal.
preference.getTitle().equals(getString(R.string.settings_diverse_about))
I had the same problem. I tried to set up a onPreferenceChangeListener but that was wrong. What I really needed was an onSharedPreferenceListener.
OnSharedPreferenceChangeListener listener;
Write this in the onCreate() of your Preference activity:
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// Here you can work.
// IMPORTANT: Beware that at this point the preference has already been changed!
}
};
SharedPreferences prefs = getSharedPreferences(getPackageName() + "_preferences", MODE_PRIVATE);
prefs.registerOnSharedPreferenceChangeListener(listener);
I have a class that extends PreferenceActivity and shows the preference screen of my app. Is it possible to check if any changes were made to the preferences?
This helps...
http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html
Other related post:
SharedPreferences.onSharedPreferenceChangeListener not being called consistently
public class PreferenceClass extends PreferenceActivity {
OnSharedPreferenceChangeListener listener;
public void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = this.getSharedPreferences("settings", 0);
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
int flag = 1;
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
super.onCreate(null);
addPreferencesFromResource(R.xml.settings);
}
}
Do
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// your stuff here
}
};
In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method.
That's what I do and I never have a problem.
Else it's your conditional checking in the listener that is at fault. Post the code.
EDIT:
From the code you posted, you should make prefs a class member variable so it has a global scope.
And do prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); instead of getSharedPreferences because you haven't created that file.
To create a file you need to use PreferenceManager. To get a PreferenceManager, use Activity.getPreferenceManager().
In your PreferenceActivity class, implement the SharedPreferences.OnSharedPreferenceChangeListener interface. Add the required onSharedPreferenceChanged method to your class and register it in the onCreate.
See sample code here:
public class MyPreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fw_preferences); //deprecated
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// handle the preference change here
}
}