fengdai
06/20/2022, 7:42 AMAndroidView recreate view when LocalContext.current changed?fengdai
06/20/2022, 8:01 AMLocalContext.current change?Filip Wiesner
06/20/2022, 8:03 AMLocalContext is StaticCompositionLocal which means that if it changes, it should recompose the whole composition tree.
Unlike compositionLocalOf, reads of a staticCompositionLocalOf are not tracked by the composer and changing the value provided in the CompositionLocalProvider call will cause the entirety of the content to be recomposed instead of just the places where in the composition the local value is used.
fengdai
06/20/2022, 8:22 AMAndroidView to invoke its factory lambda again to recreate view. Am I right?Filip Wiesner
06/20/2022, 8:24 AMAndroidView is destroyed and created again.fengdai
06/20/2022, 8:37 AMfengdai
06/20/2022, 8:44 AMCompositionLocalProvider where the value is provided.Filip Wiesner
06/20/2022, 9:23 AMAndroidView depends on the context so if the context changes (and that is a big if), it should be recreated.
Either way I would think that this is something the creators of Compose and more specifically AndroidView thought of and you should not have to think about things like this.
Or do you have some specific use case that relies on this behavior?fengdai
06/20/2022, 9:48 AMtad
06/20/2022, 4:57 PMtad
06/20/2022, 4:59 PMsetContent and similar create a ComposeView.fengdai
06/21/2022, 2:25 AMval localContext = LocalContext.current
var context by remember { mutableStateOf(localContext) }
CompositionLocalProvider(
LocalContext provides context
) {
Log.d("AndroidView", "compose: context - ${LocalContext.current}")
AndroidView(factory = { context ->
TextView(context).apply {
Log.d("AndroidView", "create view: ${this.hashCode()}")
}
}) {
Log.d("AndroidView", "update view: ${it.hashCode()}")
}
}
LaunchedEffect(key1 = Unit) {
delay(3000)
context = ContextWrapper(context)
}
And log is:
compose: context - sample.MainActivity@da29b84
create view: 162893151
update view: 162893151
compose: context - android.content.ContextWrapper@d588329fengdai
06/21/2022, 2:26 AMLocalContext.current will never be changed?
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1655712069528009?thread_ts=1655710925.339069&cid=CJLTWPH7Stad
06/21/2022, 2:44 AMLocalContext is an immutable property of the composition, or; structure your code to make no assumptions about the runtime value of LocalContext. In practice, both stances are the same, because staticCompositionLocalOf will trigger a recomposition of everything, as it's set within setContent on Android; therefore you can't really avoid being recomposed.tad
06/21/2022, 2:48 AMfengdai
06/21/2022, 3:00 AMLocalContext for me.fengdai
06/21/2022, 3:14 AMWillSince: •recreate view whenAndroidViewchanged?LocalContext.current
LocalContext.current changes triggers recomposition.
• Recomposition doesn’t trigger AndroidView to invoke its factory lambda again.
So, the answer is no.
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1655778343809389?thread_ts=1655710925.339069&cid=CJLTWPH7S