'How to maintain Lottie animation progress across different recompositions

I'm trying to use Lottie compose for playing animation in compose. But the animation starts from the very beginning for all recompositions. I wish to maintain the current playback and not restart the animation for each recomposition. Here is my current code

@Composable
fun Loader() {
    val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.main))
 LottieAnimation(composition)
}


Solution 1:[1]

You need to save animation progress outside of the composable functions, that will be recomposed

  @Composable
fun ParentComposable() {
    val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.main))
    val animationProgress by animateLottieCompositionAsState(composition = composition)

    ChildComposable(animationProgress = animationProgress, composition = composition)
}

@Composable
fun ChildComposable(animationProgress: Float, composition: LottieComposition?) {
    ...
    Loader(progress = animationProgress, composition = composition)
    ...
}

@Composable
fun Loader(animationProgress: Float, composition: LottieComposition?) {
    ...
    LottieAnimation(composition, animationProgress)
    ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mirhack