In Compose, how does updateTransition drive several values from one bouquet-card state, and how does rememberInfiniteTransition differ for a pulsing preparing indicator?
answer
- one target state, many children
- transitionSpec per segment, isTransitioningTo
- child specs must be finite
- infinite runs while in composition
- infiniteRepeatable with RepeatMode.Reverse
basics
~20 supdateTransition holds one target state and runs child animations such as animateDp and animateColor toward per-state values together, each with its own finite spec. rememberInfiniteTransition has no target state: its children loop with infiniteRepeatable for as long as they are composed.
solid answer
~40 s`updateTransition(targetState = cardState, label = "card")` remembers a `Transition`. Child animations such as `transition.animateDp { state -> ... }` and `transition.animateColor { ... }` map each state to a value, and all of them start together whenever the target changes. A `transitionSpec` lambda gets the `Segment`, so `Collapsed isTransitioningTo Selected` can choose `keyframes`, `tween` or `spring` per direction. Child specs must be a `FiniteAnimationSpec`, and the default is `spring()`. The transition is done when `currentState == targetState`. `rememberInfiniteTransition(label = ...)` has no states: `animateFloat(initialValue, targetValue, infiniteRepeatable(tween(600), RepeatMode.Reverse))` starts when it enters composition and stops only when it leaves. So put the preparing pulse inside the branch that is shown only while the shop is confirming.
code
kotlin · 17 linesval transition = updateTransition(targetState = cardState, label = "bouquetCard")
val elevation by transition.animateDp(label = "elevation") { state ->
if (state == CardState.Selected) 8.dp else 1.dp
}
val border by transition.animateColor(
transitionSpec = {
if (CardState.Collapsed isTransitioningTo CardState.Selected) {
keyframes {
durationMillis = 400
Color.Yellow at 150
}
} else {
tween(durationMillis = 200)
}
},
label = "border",
) { state -> if (state == CardState.Selected) Color.Magenta else Color.LightGray }go deeper
Recall that updateTransition animates several values from one state, and that rememberInfiniteTransition loops values with infiniteRepeatable.
Explain child animations with targetValueByState, per-Segment transitionSpec with isTransitioningTo, the FiniteAnimationSpec bound, and currentState equalling targetState as completion.
Scope infinite transitions to the composable that needs them, and know they pause when the window stops and skip to the end when the animation scale is 0.
Set a convention: one Transition per component state machine, with infinite loops scoped to transient states. That keeps motion consistent and keeps idle screens from burning frames.
## updateTransition: one state, many animated values A **`Transition`** manages several child animations that run **simultaneously** between states. `updateTransition(targetState, label)` creates and remembers one and moves it to the new target on every recomposition. States can be any type, but an `enum` keeps them type-safe: - `enum class CardState { Collapsed, Selected }` - `val transition = updateTransition(targetState = cardState, label = "bouquetCard")` Each **child animation** is declared with an `animate*` extension: `animateDp`, `animateFloat`, `animateColor`, `animateOffset`, `animateRect` and others, or `animateValue` with a converter. Each takes a `targetValueByState` lambda that returns the value for a given state. When the target changes, **all children start together** toward their new values. Compared with several separate `animate*AsState` calls, a transition gives you: 1. **One source of truth.** Elevation, border colour and scale cannot drift apart, because they read one target. 2. **One completion signal.** `transition.currentState` stays at the initial state until every child finishes, then becomes equal to `transition.targetState`. `isRunning` reports whether any child is still animating. 3. **Per-direction specs.** A `transitionSpec` lambda runs on a `Transition.Segment`, which exposes `initialState`, `targetState` and the `isTransitioningTo` infix. Selecting can use a `keyframes` flash while deselecting uses a short `tween`. 4. **Composition with other APIs.** `transition.AnimatedVisibility(visible = { it == CardState.Selected })` and `transition.AnimatedContent { ... }` hoist those animations into the same transition. Child `transitionSpec` lambdas return a **`FiniteAnimationSpec`**. The default is `spring()`, with a type-specific visibility threshold such as `Dp.VisibilityThreshold`. `infiniteRepeatable` produces an `InfiniteRepeatableSpec`, which is only an `AnimationSpec`, so it does not compile as a transition child spec. To start in one state and animate to another as soon as the card appears, create a `MutableTransitionState(CardState.Collapsed)`, set its `targetState`, and pass it to `rememberTransition`. ## Specs you choose per child | Spec | Defaults | Typical use on the card | |---|---|---| | `spring(dampingRatio, stiffness)` | `Spring.DampingRatioNoBouncy`, `Spring.StiffnessMedium` | elevation and scale that may be interrupted | | `tween(durationMillis, delayMillis, easing)` | 300 ms, no delay, `FastOutSlowInEasing` | a colour change timed to a fixed duration | | `keyframes { durationMillis = 400; Color.Yellow at 150 }` | `durationMillis` 300 if unset | a highlight flash at a set moment | ## rememberInfiniteTransition: motion with no end state A "preparing" pulse has no target state. It loops until the shop confirms. **`InfiniteTransition`** is built for that. `rememberInfiniteTransition(label = "preparing")` creates one, and its children are declared with `animateFloat`, `animateColor` or `animateValue`. Each takes an `initialValue`, a `targetValue` and an **`InfiniteRepeatableSpec`**: - `animationSpec = infiniteRepeatable(animation = tween(600, easing = LinearEasing), repeatMode = RepeatMode.Reverse)` `infiniteRepeatable` wraps a **duration-based** spec (`tween`, `keyframes`, `snap`), not a spring. `RepeatMode.Restart`, the default, jumps back to the start each cycle. `RepeatMode.Reverse` plays back and forth, which suits a pulse. The documentation states the lifetime: children **start as soon as they enter composition and do not stop until they are removed**. That has practical consequences: - **Place it in the branch that is shown while preparing.** If the infinite transition sits at screen level and you merely hide the dot, it keeps producing frames for nothing. Put it inside the composable that exists only while the order is pending, for example inside an `AnimatedVisibility` or `when` branch. - **Still composed means still running.** A dot in a scrolled-away part of a non-lazy `Column` is still composed, so it keeps animating. - **Window stops pause it.** When the hosting window's lifecycle reaches `ON_STOP`, Compose pauses the frame clock, which pauses frame-driven animations until `ON_START`. - **Animations turned off.** When the system animation duration scale is 0, an infinite transition skips its children to their end values and waits until the scale becomes non-zero. ## Labels and tooling Both `updateTransition` and `rememberInfiniteTransition` take a `label`, and every child animation takes one too. Android Studio's animation tooling uses these labels to list each transition and its children, so you can scrub a card's Collapsed-to-Selected change frame by frame, or inspect the pulse's values. Unlabelled animations show up under generic default names such as "FloatAnimation", which makes a screen with several of them hard to read. ## Choosing between them - A value with a finite destination that depends on state: use `updateTransition`, or `animate*AsState` for a single value. - A loop with no destination: use `rememberInfiniteTransition`. - A value that a gesture must grab, stop or fling: use `Animatable`, which gives you imperative control.
- Why not just use three animate*AsState calls for the card's elevation, border and scale?They would work, but each would animate independently, with no shared completion signal and no per-direction spec. A single `updateTransition` keeps them tied to one target, lets `transitionSpec` choose a spec per `Segment`, and reports completion when `currentState` equals `targetState`.
- Can you give a Transition child an infiniteRepeatable spec to make the selected card glow forever?No. Transition child specs are `FiniteAnimationSpec`, and `infiniteRepeatable` returns an `InfiniteRepeatableSpec`, which is not finite. Model the glow as a separate `rememberInfiniteTransition` composed only while the card is selected.
- Each updateTransition child animation needs its own target state variable.
- infiniteRepeatable can be used as a Transition child's transitionSpec.
- An infinite transition stops by itself once nothing on screen reads it.
- infiniteRepeatable accepts a spring as its animation.
- A transition is finished as soon as its targetState is assigned.