Saran
Saran

Reputation: 3885

Apply new Locale to application when changed from PreferenceFragment

My preferences are setup via PreferenceActivity which in onBuildHeaders() loads the PreferenceFragments from <preference-headers> xml resource.

One of those fragments holds a ListPreference with languages.

OnSharedPreferenceChangeListener is correctly (un)registered and onSharedPreferenceChanged() is correctly called. When the onSharedPreferenceChanged() is called, first I update the default locale:

    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    mContext.getResources().updateConfiguration(config, mContext.getResources().getDisplayMetrics());

then I update titles and summaries of preferences of this fragment (which correctly pull strings from newly set locale).

Going back triggers onResume in PreferenceActivity:

@Override
protected void onResume() {
    super.onResume();
    invalidateHeaders();
}

which forces preference fragments to correctly display their titles in newly set locale.

All that is fine, but what troubles me is that:

How can I fix that?

(minSdkVersion is 15)

Upvotes: 0

Views: 508

Answers (1)

Saran
Saran

Reputation: 3885

Solved it myself. Here are all the aspects...

1) PreferenceFragment title is not changed

When updating title and summaries in PreferenceFragment also update PreferenceActivity title (which actually provides title for the PreferenceFragment):

getActivity().setTitle(getString(R.string.title_activity_settings));

2) PreferenceActivity title is not changed

In onResume() of the PreferenceActivity, update its title:

setTitle(getString(R.string.title_activity_settings));

3) all text of MainActivity remains unchanged

Track locale (String) in onCreate() of MainActivity and compare it to current locale (String) in onResume(). If different, restart the activity with:

Intent intent = getIntent();
finish();
startActivity(intent);

Upvotes: 1

Related Questions