Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple urls doesn't start animation from the beginning #2516

Closed
davidcrottymonzo opened this issue Jul 4, 2024 · 1 comment
Closed

Multiple urls doesn't start animation from the beginning #2516

davidcrottymonzo opened this issue Jul 4, 2024 · 1 comment

Comments

@davidcrottymonzo
Copy link

davidcrottymonzo commented Jul 4, 2024

Minimal example to reproduce:

Scaffold(modifier = Modifier.fillMaxSize()) { padding ->
                    var url by remember { mutableStateOf("https://assets3.lottiefiles.com/packages/lf20_FyWhBU.json") }
                    Column(Modifier.padding(padding)) {
                        Button(onClick = {
                            url = if (url == "https://assets3.lottiefiles.com/packages/lf20_FyWhBU.json") {
                                "https://assets2.lottiefiles.com/packages/lf20_NBpLbW.json"
                            } else {
                                "https://assets3.lottiefiles.com/packages/lf20_FyWhBU.json"
                            }

                        }) {
                            Text("Change animation")
                        }
                        Animation(
                            url = url,
                        )
                    }

                }

@Composable
fun Animation(url: String, isPlaying: Boolean = true, loop: Boolean = true) {
    val spec = LottieCompositionSpec.Url(url)
    val composition = rememberLottieComposition(spec)
    val progress by animateLottieCompositionAsState(
        composition = composition.value,
        isPlaying = isPlaying,
        restartOnPlay = false,
        iterations = if (loop) LottieConstants.IterateForever else 1
    )

Log.d("Animation", "url: $url progress: $progress")

    composition.value?.let {
        LottieAnimation(
            composition = composition.value,
            progress = { progress },
            modifier = Modifier
        )
    }
}

Describe the bug

When switching to a new url, the animation in the new url does not start from the beginning. You can see this in the logs:

Screenshot 2024-07-04 at 16 00 07

Steps To Reproduce
Steps to reproduce the behavior:

  1. Copy above code into sample app.
  2. Run, observe animations not starting from start frames when tapping change animation button & in logs

Additional info

The main issue seems to be not being able to trigger this inside animateLottieCompositionFromState

        if (isPlaying && !wasPlaying && restartOnPlay) {
            animatable.resetToBeginning()
        }

For now you can workaround this by wrapping the entire block with a key, as remember doesn't work due to lottie holding onto these states via remember {} functions

@gpeal
Copy link
Collaborator

gpeal commented Sep 29, 2024

If you wrap your Animation(…) call with a key, you can reset it when the url changes. Something like:

key(url) {
    Animation(…)
}

@gpeal gpeal closed this as completed Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants