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
Animation is stopped after has entered background state #412
Comments
The same issue. |
it's not expected behavior but it's a side effect of trying to make the library as performant as possible. We definitely need to keep track within the library if the animation is running when the component is unmounted and reset it when the component is mounted again. |
Any idea how to fix this issue? |
@lorenc-tomasz we will fix this on the library. For the time being you can use @houmark's workaround:
|
Here's the workaround: export default class YourComponentName extends Component {
state = {
appState: null,
}
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = nextAppState => {
const { appState } = this.state;
if (appState && appState.match(/inactive|background/) && nextAppState === 'active') {
if (this._animation) {
this._animation.play();
}
}
this.setState({ appState: nextAppState });
};
_setAnimation = ref => {
this._animation = ref;
};
render() {
return (
<LottieView
source={require('../assets/lottie/animation.json')}
autoPlay
loop={true}
ref={this._setAnimation}
/>
);
}
}; |
Thanks :) |
Thanks @houmark that seems to work fine for me too. Here's a TS version: import React, { Component } from 'react'
import { AppState, AppStateStatus } from 'react-native'
import Lottie from 'lottie-react-native'
type extractComponentPropsType<Type> = Type extends Component<infer X>
? X
: null
// See https://github.com/react-native-community/lottie-react-native/issues/426
type LottieProps = extractComponentPropsType<Lottie>
interface State {
appState: AppStateStatus
}
// Because Lottie has a bug that stops animation when in background (ios)
// See https://github.com/react-native-community/lottie-react-native/issues/412
export class LottieAnimation extends Component<LottieProps, State> {
state: State = {
appState: AppState.currentState
}
ref = React.createRef<Lottie>()
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange)
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange)
}
_handleAppStateChange = (nextAppState: AppStateStatus) => {
const { appState } = this.state
this.setState({ appState: nextAppState })
if (appState.match(/inactive|background/) && nextAppState === 'active') {
if (this.ref.current) {
this.ref.current!.play()
}
}
this.setState({ appState: nextAppState })
}
render() {
return <Lottie ref={this.ref} autoPlay={true} {...this.props} />
}
} |
Just want to point out that this bug has been reported when working on Expo in dev/prod. Our build APK on testflight does not seem to be affected. |
Hi, I'm also facing the same issue, I tried @houmark's workaround, But when my application becomes active after goes to background state, I got this error.
If i use this code
|
Thanks @houmark for the fix! Quick comment: you missed a check for appState being null in the initial case. This line: |
@kthaas Yeah, so I did some copy pasting from my original code that has more logic than this basic example, but thanks for the update, I will update the example. |
What one can do on top of this example is also checking if the animations are loop To do that check:
|
Thanks all, I have just want to mark final solution for this problem in IOS platform
|
In ios version, there is a |
you mean on the native SDK? because I don't find this param documented in this repo |
I believe so, just try typing it in xcode. |
it seems that latest version not solve the bug, why closed it... |
Fix AnimationUIblock on scroll or when mounting and unmounting components in ios
Fix lottie-react-native#412 animation stops when ui block
Description
Animation is stopped after app has entered background state. This seems to mainly be a problem on iOS, on Android I was not able to reproduce it easily in the emulator, but it may also be an issue in Android.
Steps to Reproduce
Expected behavior: [What you expected to happen]
Animation is still running or starts again.
Actual behavior: [What actually happened]
Animation is stopped.
Versions
2.5.10
I'm working around this by setting a
ref
on the<LottieView>
then using an event listener I'm playing the animation again if and when the animation is looping and mounted in the current view/screen.Is this expected behavior? It becomes a bit tedious to keep track of
n
animations inside the component in order toplay()
them again when the app becomes active, and I would expect that a looping animation would continue after the app has been in the background/sleeping.The text was updated successfully, but these errors were encountered: