How to restart the application after onSharedPreferenceChanged has been called?
ActivityPreferences.java:
[..]
public static class FragmentPreferences extends PreferenceFragment {
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences);
final SharedPreferences sp = context.getSharedPreferences("PREFERENCE_KEY", Context.MODE_PRIVATE);
SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
context.finish();
startActivity(new Intent(ActivityPreferences.this, ActivityLogin.class));
}
};
}
}
That's what I tried, but I can't call finish()
.
I want to change the theme, if the listpreference s1_theme
has been set to light/dark:
ActivityMain.java:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Context context = getApplicationContext();
final SharedPreferences sp = context.getSharedPreferences("PREFERENCE_KEY", Context.MODE_PRIVATE);
if (sp.getString("s1_theme", "-1") == "light"){
setTheme(R.style.AppTheme_BaseLight);
} else {
setTheme(R.style.AppTheme_BaseDark);
}
setContentView(R.layout.activity_main);
[..]