【发布时间】:2019-10-31 12:52:08
【问题描述】:
我正在开发一个支持多种语言的应用程序,因此当用户更改语言时,我们会使用 Intent.FLAG_ACTIVITY_CLEAR_TOP、FLAG_ACTIVITY_CLEAR_TASK 和 FLAG_ACTIVITY_NEW_TASK 标志启动启动活动,然后完成语言活动。
Intent mIntent = new Intent(this, SplashActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(mIntent);
finish();
我们创建了包装类来包装应用程序资源
class AppResources(private val context: Context): IAppResources {
override fun getString(resId: Int): String {
Logger.i("Current locale language is: ${context.resources.configuration.locale.language}")
return context.getString(resId)
}
override fun getStringArray(resId: Int): Array<String> {
Logger.i("Current locale language is: ${context.resources.configuration.locale.language}")
return context.resources.getStringArray(resId)
}
}
然后我们像这样使用 Koin 注入这个类
factory<IAppResources> {
Logger.i("Current androidContext language is: ${androidContext().resources.configuration.locale.language}")
AppResources(androidContext())
}
问题是,当我们从资源中获取字符串值时,我们得到了错误的本地化,因为 Koin 已经从以前的 android context 和 AppResources 类开始,已经用旧的 context 初始化。
所以有任何解决这个问题的建议。
【问题讨论】:
标签: android kotlin dependency-injection koin onconfigurationchanged