0

In below code I have used animateDpAsState and it recompose function only once but button recompose multiple times.

@Composable
fun MoveText() {
    LogCompositions("JetpackCompose.app", "========CustomText function")
    var visible by remember { mutableStateOf(true) }
    val iconOffsetAnimation: Dp by animateDpAsState(
        if (visible) 13.dp else 0.dp,
        tween(1000), label = ""
    )
    val textOffsetAnimation: Dp by animateDpAsState(
        if (visible) 19.dp else 5.dp,
        tween(1000), label = ""
    )

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(start = 16.dp, top = 16.dp)
    ) {
        Column {
            Icon(
                modifier = Modifier
                    .graphicsLayer { translationY = iconOffsetAnimation.toPx() },
                imageVector = Icons.Default.ShoppingCart,
                tint = Color.Black,
                contentDescription = null,
            )
            Text(
                modifier = Modifier
                    .graphicsLayer { translationY = textOffsetAnimation.toPx() },
                text = "Hello, Anna",
                fontSize = 20.sp,
                color = Color.Black,
            )
        }
        Button(
            modifier = Modifier
                .padding(top = 30.dp),
            onClick = {
                visible = !visible
            },
        ) {
            LogCompositions("JetpackCompose.app", "Button")
            Text(text = "Move Text")
        }
    }

}

class Ref(var value: Int)

// Note the inline function below which ensures that this function is essentially
// copied at the call site to ensure that its logging only recompositions from the
// original call site.
@Composable
inline fun LogCompositions(tag: String, msg: String) {

        val ref = remember { Ref(0) }
        SideEffect { ref.value++ }
        Log.d(tag, "Compositions: $msg ${ref.value}")

}

As in below image you can see function is recompose only 1 time but button recomposition happened 11 times.

enter image description here

Don't know why this happy. Is there any thing that I did wrong.

I don't know this is normal or we can fix this problem. Any help would be appreciated. Thanks.

As per @Gabriele Mariotti suggestion, I added LogCompositions in Button as well as in MoveText() function.

Surprisingly in logs I have seen that Button is not recompose that is as expected behavior.

Now I had scratch my head why in layout inspector it shown 11 times count of button recomposition.

Because every time adding logs to check recomposition is not feasible or best way as per my point of view.

Can anyone suggest on this why this is happening? Thanks

Darshan Mistry
  • 3,294
  • 1
  • 19
  • 29
  • 1
    I am not so sure about that count. Use the [LogCompositions function](https://www.jetpackcompose.app/articles/donut-hole-skipping-in-jetpack-compose) reported here to double check the recomposition count. – Gabriele Mariotti Mar 14 '23 at 12:01
  • @GabrieleMariotti Thanks for sharing blog, I added LogCompositions in Button as well as in MoveText() function.Surprisingly in logs I have seen that Button is not recompose that is as expected behavior. – Darshan Mistry Mar 14 '23 at 12:41
  • 1
    @GabrieleMariotti Now I had scratch my head why in layout inspector it shown 11 times count of button recomposition. Because every time adding logs to check recomposition is not feasible or best way as per my point of view. – Darshan Mistry Mar 14 '23 at 12:42
  • 1
    I agree but I should check how the counting in the Layout inspector is done. – Gabriele Mariotti Mar 14 '23 at 13:01
  • @GabrieleMariotti Thanks for super prompt response. Please share if you find any useful things for this. – Darshan Mistry Mar 14 '23 at 13:06
  • 1
    I don't know how many times i answered layout inspector doesn't show correct numbers. Best option is either use logging or using color change on every composition. https://stackoverflow.com/a/75152405/5457853 aksı comment section of this answer as well. https://stackoverflow.com/a/74700668/5457853 – Thracian Mar 14 '23 at 16:11
  • @Thracian Thanks for sharing post with different options to check recomposition. Is there anywhere documented on Android site that layout inspectors why not showing correct count. If you find than please share. Thanks. – Darshan Mistry Mar 14 '23 at 18:09
  • 1
    I don't know why it's not showing correct numbers but when someone tells me it's recomposing i always ask them to show me color change or logs. Never trust layout inspector alone especially with LazyColumn/Row recomposition counts – Thracian Mar 14 '23 at 18:20

0 Answers0