what is the correct way to force existing activities to reload using the new local
have a settings dialog/activity where I allow the use开发者_运维百科r to change the locale. Within that activity i call
Resources res = ctx.getResources();
// Change locale settings on the device
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase(),
coutry_code.toUpperCase());
res.updateConfiguration(conf, dm);
and call onChanged() for my settings ListView... everything changes perfectly. Very nice. Then I hit the back button to go back to the previous activity thinking everything should be switched; nope. Everything is still in the previous locale. So I added the above code in the onResume() of the activity thinking that would do it; nope. Also, when I click the menu button of this activity again (not first time) I do not get called in onCreateOptionsMenu() it just displays the previous menu. My question is what is the correct way to force existing activities to reload using the new local?
I don't know exactly, but this seems to be an overhead. The standart way of swithicng locales is through the system settings. Let the user switch the locale there and just use Locale.getDefault()
in your code.
An OnSharedPreferenceChangeListener in the parent activity is the correct place to respond to the change in preferences. As for refreshing the UI, I'm not entirely sure. We're told to let android manage the lifecycle of our activities for us but imho that's not always going to work. One option is to call onCreate() from onSharedPreferenceChanged.
Edit: Sorry, this listener probably won't work as you're not talking about a shared preference.
精彩评论