1

I'm trying to create a simple Settings Activity with a single setting to change the language of the App.

<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:title="@string/locale">

    <ListPreference
        android:key="lang"
        android:title="@string/language"
        android:summary="@string/languageDesc"
        android:entries="@array/languages"
        android:entryValues="@array/languageValues"
        android:defaultValue="@string/locale_en"/>

</PreferenceCategory>

public class TCPreferenceActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (key.equals("lang")) {

        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("lang", sharedPreferences.getString(key, "en_US"));
        editor.commit();

        settings();
    }
}

public void settings() {
    Intent intent = new Intent(this, TCPreferenceActivity.class);
    intent.putExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT, TCPreferenceFragment.class.getName());
    intent.putExtra(PreferenceActivity.EXTRA_NO_HEADERS, true);
    startActivity(intent);
}


@Override
protected void attachBaseContext(Context newBase) {

    SharedPreferences pref =  newBase.getSharedPreferences("lang", MODE_PRIVATE);

    String lang = pref.getString("lang", null);

    Locale locale = new Locale(lang);

    Context context = TCContextWrapper.wrap(newBase, locale);
    super.attachBaseContext(newBase);
}

}

When I debug the activity, i see the updated value being received in the method onSharedPreferenceChanged.

However when i call the Intent to reload the activity, with the context wrapper in order to change the language, the value received from the call to newBase.getSharedPreferences("lang", MODE_PRIVATE) is still the original unchanged value.

When i click again on the preference in the interface, i see that the value hash changed.

Do I need to save the value?

Why doesn't it changed in the SharedPreferences class?

I'm trying to replicate what0s being done in the exemple here:

Android context.getResources.updateConfiguration() deprecated

Thanks in advance.

Joao Neto
  • 310
  • 3
  • 18
  • 1
    You're using two different SharedPreferences, maybe? One for that preferences Activity, the other for that newBase context – OneCricketeer May 30 '17 at 13:41
  • I guess so. But this is a design patter i copied for the locale modification via preferences. I detect the preference change, and I reload the activity. Then the reloaded activity loads the new locale value from preferences and changes the locale in the configuration wrapper. https://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated – Joao Neto May 30 '17 at 14:39
  • Try adding logs to confirm that `editor.commit();` is being executed. I mean, `onSharedPreferenceChanged()` is being called and the condition is true. – Kamran Ahmed May 30 '17 at 15:08

2 Answers2

1

Try changing:

SharedPreferences pref =  newBase.getSharedPreferences("lang", MODE_PRIVATE);

To:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(TCPreferenceActivity.this);

For what ever reason, you are loading up private "lang" preferences, which im pretty sure you are not saving to. Use the default preferences instead which the activity should be by default using else where.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • 1
    That's exactly it! Thank you very much. It's my first time using SharedPreferences so I copied snippets of code here and there. I now know the difference between the default in memory preferences and committing them to file. Thanks! – Joao Neto May 31 '17 at 08:28
0

I would rather that you user apply() instead of commit().

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk.

Do this;

 SharedPreferences.Editor editor = sharedPreferences.edit();
 editor.putString("lang", sharedPreferences.getString(key, "en_US"));
 editor.apply();
Thadeus Ajayi
  • 993
  • 10
  • 16