Pause Lottie when visibility changes
onAttachedToWindow and onDetachedFromWindow aren't sufficient for LottieAnimationView to properly pause and resume. We must do the same from onVisibilityChanged as well or we risk Lottie animating when the app is in the background.
Fixes
Thanks
@@ -21,6 +21,7 @@ import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.JsonReader;
import android.util.Log;
+import android.view.View;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.value.LottieFrameInfo;
@@ -71,6 +72,7 @@ import java.util.Set;
private final LottieDrawable lottieDrawable = new LottieDrawable();
private String animationName;
private @RawRes int animationResId;
+ private boolean wasAnimatingWhenVisibilityChanged = false;
private boolean wasAnimatingWhenDetached = false;
private boolean autoPlay = false;
private RenderMode renderMode = RenderMode.Automatic;
@@ -228,6 +230,20 @@ import java.util.Set;
setRepeatCount(ss.repeatCount);
}
+ @Override
+ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
+ if (visibility == VISIBLE && wasAnimatingWhenVisibilityChanged) {
+ resumeAnimation();
+ } else {
+ if (isAnimating()) {
+ wasAnimatingWhenVisibilityChanged = true;
+ pauseAnimation();
+ } else {
+ wasAnimatingWhenVisibilityChanged = false;
+ }
+ }
+ }
+
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (autoPlay && wasAnimatingWhenDetached) {
