问题描述
我已经实现自定义的语言环境选择在大约一年前,但后4.1版本的用户开始抱怨在不断的活动闪烁。这里是code我使用(来自不同的,所以答案编译):
I have implemented custom locale selection about a year ago but after 4.1 release users start to complain on constant activity blinking. Here is code I'm using (compiled from different SO answers):
public final class TestApplication extends Application
{
private Locale desiredLocale = new Locale("ru-RU");
@Override
public void onCreate() {
super.onCreate();
updateLocale(new Configuration());
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
updateLocale(newConfig);
}
private void updateLocale(Configuration newConfig) {
newConfig.locale = desiredLocale;
Locale.setDefault(desiredLocale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
应用程序只包含一个空的活动,这是由Android的重建每一秒后,我改变设备的方向。 这里是样品的来源。
它看起来像所有使用这种技术的应用变得无效。什么是正确的做法?
It looks like all applications which use this technique became invalid. What is the correct approach?
推荐答案
该行造成的解决方案失败:
This line caused solution to fail:
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
正确的(至少它的工作原理)实现在这里被定义 http://stackoverflow.com/a/14010044/554336 :
Correct (at least it works) implementation is defined herehttp://stackoverflow.com/a/14010044/554336 :
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
所以,新的配置实例应创建的每个时间。
So new configuration instance should be created every time.
这篇关于活动是在Android的语言环境改变后闪烁4.1+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!