https://kotlinlang.org logo
#compose
Title
# compose
a

Adib Faramarzi

11/20/2021, 10:35 AM
Is there any APIs for being notified about an animation being finished in
AnimatedVisibility
? (like
onFinishedListener
)
m

maciejciemiega

11/20/2021, 5:46 PM
It's not a listener, but you can react to transition state. Your layout would be recomposed and you can do something then - I'm not sure what kind of action you want to perform, but maybe this will help you:
Copy code
// yourContent is not visible by default
val transitionState = remember { MutableTransitionState(false) }

// trigger it to be visible at some point
transitionState.targetState = isVisible

AnimatedVisibility(transitionState) {
    YourContent()
}

if (transitionState.isIdle && transitionState.targetState) {
    // here your content is visible and animation is idle
}
❤️ 1
You can also play with
snapshotFlow
to convert this state to
Flow
and invoke some action on state change: https://developer.android.com/jetpack/compose/side-effects#snapshotFlow
Copy code
LaunchedEffect(key1 = transitionState) {
    snapshotFlow { transitionState.isIdle && transitionState.targetState }
        .filter { it == true }
        .collect {
            // some action
        }
}
a

Adib Faramarzi

11/21/2021, 7:13 AM
I did not know about the TransitionState. It might help. Thanks.
👍 1
3 Views