更新配置后如何触发重组? [英] How to trigger recomposition after updateConfiguration?

查看:0
本文介绍了更新配置后如何触发重组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望动态更改我的应用程序语言,而无需重新启动Activity以使结果生效。我现在要做的是添加一个可变的Boolean状态,它是Switch,由所有Text元素使用。

为了更改语言,我在可点击回调中调用了以下代码(我将框用作虚拟对象,只是为了测试):

val configuration = LocalConfiguration.current
val resources = LocalContext.current.resources
Box( 
    modifier = Modifier
        .fillMaxWidth()
        .height(0.2.dw)
        .background(Color.Red)
        .clickable {
            
            // to change the language
            val locale = Locale("bg")
            configuration.setLocale(locale)
            resources.updateConfiguration(configuration, resources.displayMetrics)
            viewModel.updateLanguage()
        }
) {
}

然后它使用updateLanguage()方法切换语言值

@HiltViewModel
class CityWeatherViewModel @Inject constructor(
    private val getCityWeather: GetCityWeather
) : ViewModel() {

    private val _languageSwitch = mutableStateOf(true)
    var languageSwitch: State<Boolean> = _languageSwitch

    fun updateLanguage() {
        _languageSwitch.value = !_languageSwitch.value
    }
}

问题是,为了更新每个Text可组合元素,我需要将viewmodel传递给使用Text的所有子代,然后使用一些错误的逻辑强制每次更新,视图模型中会发生一些更改。

@Composable
fun SomeChildDeepInTheHierarchy(viewModel: CityWeatherViewModel, @StringRes textResId: Int) {
 
    Text(
        text = stringResource(id = if (viewModel.languageSwitch.value) textResId else textResId),
        color = Color.White,
        fontSize = 2.sp,
        fontWeight = FontWeight.Light,
        fontFamily = RobotoFont
    )
}

它可以工作,但这是一些非常糟糕的逻辑,并且代码非常难看!是否有使用Jetpack Compose动态更改Locale的标准方法?

推荐答案

最简单的解决方案是在更改配置后重新创建活动:

val context = LocalContext.current
Button({
    // ...
    resources.updateConfiguration(configuration, resources.displayMetrics)
    context.findActivity()?.recreate()
}) {
    Text(stringResource(R.string.some_string))
}

findActivity

fun Context.findActivity(): Activity? = when (this) {
    is Activity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}

如果出于某种原因您不想这样做,您可以使用如下所示的新配置覆盖LocalContext

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            val context = LocalContext.current
            CompositionLocalProvider(
                LocalMutableContext provides remember { mutableStateOf(context) },
            ) {
                CompositionLocalProvider(
                    LocalContext provides LocalMutableContext.current.value,
                ) {
                    // your app
                }
            }
        }
    }
}

val LocalMutableContext = staticCompositionLocalOf<MutableState<Context>> {
    error("LocalMutableContext not provided")
}

在您的视图中:

val configuration = LocalConfiguration.current
val context = LocalContext.current
val mutableContext = LocalMutableContext.current
Button(onClick = {
    val locale = Locale(if (configuration.locale.toLanguageTag() == "bg") "en_US" else "bg")
    configuration.setLocale(locale)
    mutableContext.value = context.createConfigurationContext(configuration)
}) {
    Text(stringResource(R.string.some_string))
}

请注意,remember不会经历系统配置更改,例如屏幕旋转,您可能需要将选择的区域设置存储在某个地方,例如DataStore,并在提供LocalMutableContext时提供所需的配置,而不是我最初的context

附注:在这两种情况下,您都不需要在视图模型中设置标志,如果您根据documentation放置了资源,例如在values-bg/strings.xml中,等等,stringResource将开箱即用。

这篇关于更新配置后如何触发重组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆