Kevin Worth
06/10/2025, 12:36 PManimateFloatAsState, AnimatedVisibility, AnimatedContent. I need to display ComposableA when objectA is not null, ComposableB when objectB is not null, or neither when they are both null. If it were always just one or the other (if there were no "neither case") then AnimatedContent would suit me just fine. The problem is, when I transition from either one of them visible to neither one visible, the visible one just disappears suddenly. But I need it to fade out. Is there a good, clean way to do this and I'm just not seeing it, or should I file a feature request?Ian Lake
06/10/2025, 1:46 PMKevin Worth
06/10/2025, 3:08 PMAnimatedContent works. I first did this with 3 states, as you suggested, but I can also demonstrate with just the two:
fun Parent() {
AnimatedContent(
targetState = objectA != null,
) { showObjectA ->
if (showObjectA) {
if (objectA != null) {
ComposableA(objectA)
} else {
Text("A: pretend empty")
}
} else {
if (objectB != null) {
ComposableB(objectB)
} else {
Text("B: pretend empty")
}
}
}
}
In this example, we:
• start with objectA not null, so we show ComposableA
• Then objectB becomes not null, so the fadeOut starts but ComposableA disappears suddenly and the Text("A: pretend empty") shows suddenly.
• So now, Text("A: pretend empty") is the thing fading out, whereas we want ComposableA to be fading
• Then the fadeIn to show ComposableB starts
Therefore, it stands to reason when we transition from either A or B being not null to the "neither case"(both are null), it's suddenly removing the visible composable, is then suddenly showing nothing, and does a fadeOut (on that nothing).
@Ian Lake any thoughts on whether this should be expected and/or strategies for using the APIs in a better way? Again, the same problem happens when using a 3rd state to show nothing.Ian Lake
06/10/2025, 3:12 PMIan Lake
06/10/2025, 3:13 PMPair<TypeA?, TypeB?> or preferably just a data classKevin Worth
06/10/2025, 3:17 PMtargetState can and should hold everything you need (and maybe I've just missed them...). Perhaps I should create an SO question and answer. Thank you for your time!Ian Lake
06/10/2025, 6:31 PMNote that you should always use the lambda parameter and reflect it to the content.But more in depth on the actual AnimatedContent KDocs:
IMPORTANT: The targetState parameter for thelambda should always be taken into account in deciding what composable function to return as the content for that state. This is critical to ensure a successful lookup of all the incoming and outgoing content during content transform.content
Ian Lake
06/10/2025, 6:33 PM