RePlugin
RePlugin copied to clipboard
为插件配置不同语言不生效(app有切换不同语言的需求)
如下代码为插件配置语言(app有切换不同语言的需求),属于插件的activity代码
@Override
protected void attachBaseContext(Context newBase) {
Locale locale = Locale.ENGLISH;//修改为获取不同的语言
Configuration config = newBase.getResources().getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newBase = newBase.createConfigurationContext(config);
} else {
newBase.getResources().updateConfiguration(config, newBase.getResources().getDisplayMetrics());
}
super.attachBaseContext(newBase);
}
但是这样配置后语言配置不生效,非插件场景是ok的 想问下为何为啥会丢失配置呢?
遇到同样的问题。这个你有解决吗?
Replugin在打包的时候,会重写attachBaseContext(newBase)方法。这里的newBase原本是ActivityThread给Activity创建的ContextImpl对象,Replugin把它换成了PluginContext。Activity就是通过使用PluginContext中的Resources加载插件中的资源。 你这里修改配置,只是针对原本的ContextImpl的Resources的修改,所以对于插件是无效的。
正确的做法应该从newBase中获取配置,设置给PluginContext的Resource。并且这个操作应该放到super.attachBaseContext(newBase)之后执行,这个时候才能通过getBaseContext获取到PluginContext。
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
Configuration config = newBase.getResources().getConfiguration();
DisplayMetrics display = newBase.getResources().getDisplayMetrics();
getBaseContext().getResources.updateConfiguration(config, display);
}