Preference changes only happen when restarting the app
-
07-06-2021 - |
문제
I was coding an app I added a preferences menu, and it works, but changes happen only when I restart the application,anyone knows how to make changes happen whithout exitting the app??? Thanks in advance ;)
My code ( from main activity):
preferencias = preferenceManager.getDefaultSharedPreferences(TimeToSpeechActivity.this);
OnSharedPreferenceChangeListener listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
//nothing here, do I have to put anything?
}
};
preferencias.registerOnSharedPreferenceChangeListener(listener);
getPrefs();
changefont(fuente, letra);
if (boole == true) {fontcolors();}
private void getPrefs(){
fuente = Typeface.createFromAsset(getAssets() , preferencias.getString("elegirfuente", "fonts/Default.ttf"));
letra = Integer.parseInt(preferencias.getString("fontstyle", "0"));
bol = preferencias.getBoolean("randomcolors", true);
}
I have nothing put in preference activity, do i have to put anything?
Also, do I have to edit this?: (SharedPreferences prefs, String key)
I ask this because i havent created prefs
and key
varibles
Thanks in advance!!! ;)
EDIT1: Added preference activity
public class PantallaOpciones extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.opciones);
}
}
해결책
Maybe you want to use SharedPreferences like follows:
String PREFS_NAME = "com.example.sp", PREFS_TEST = "com.examples.sp.test";
SharedPreferences preferences;
preferences = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);
// This is how you instantiate a SharedPreference object.
preferences.edit().putString(PREFS_TEST,"TEST").commit();
// This way the preference is actually modified and saved.
제휴하지 않습니다 StackOverflow