How do you ensure an animated composable (currentl...
# compose-android
k
How do you ensure an animated composable (currently using multiple `animateFloatAsState`s) will animate the first time it’s added to composition but won’t animate again after it’s removed from composition and added back again?
The presenting problem is the composable is in a
LazyRow
which gets scrolled off screen and back on again, and it should not animate a second time when it’s scrolled back on screen.
j
Easiest way would be to store a flag in the parent composable or better yet, in your Viewmodel that you’d check every time the composable enter composition.
k
@Jonathan good point. If we just keep it in the parent composable, it could also get scrolled off screen and back on again, yes?
j
At some point you’ll have a parent composable that wont’ be scrolled of screen. Hoist the state there.
🤔 1
With that being said, I’d still recommend putting it in your view model. It gives you the option of restarting the animation again, even if it’s already been displayed.
k
Feels like there should be a better way. I wonder if
AnimatedVisibility
has the same problem, or not (though I’ve determined
AnimatedVisibility
is a non-starter for me, given the number of floats I need to animate in this custom animation).
j
You could use a Saveable possibly
🤔 1
You said
Row
above, do you mean
LazyRow
? From my understanding, child
Composables
in a
Row
w/ a scroll modifier shouldn’t exit composition. They will in
LazyRow
.
Or Maybe is the
Row
in a
LazyColumn
and the entire Row as well as it’s children (the one you’re animating) is leaving composition when scrolled off screen.
k
Yes, you’re correct, it is a
LazyRow
. That’s my bad (and corrected).
j
Did saveable work for you?
k
Got pulled into something else, unfortunately. We’ll see when I get back to it. Thanks for the ideas.
@Jonathan,
rememberSaveable
worked like a charm (and was way easier/lightweight than I expected). Thanks again!