Save play state in SavedState even if view was detached from the window first (#1442)

Test failed before but passes now

Fixes #1428
diff --git a/LottieSample/src/androidTest/java/com/airbnb/lottie/samples/FragmentVisibilityTests.kt b/LottieSample/src/androidTest/java/com/airbnb/lottie/samples/FragmentVisibilityTests.kt
index f929d38..fc338b9 100644
--- a/LottieSample/src/androidTest/java/com/airbnb/lottie/samples/FragmentVisibilityTests.kt
+++ b/LottieSample/src/androidTest/java/com/airbnb/lottie/samples/FragmentVisibilityTests.kt
@@ -3,6 +3,8 @@
 import android.animation.Animator
 import android.content.Intent
 import android.os.Bundle
+import android.os.Parcelable
+import android.util.SparseArray
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
@@ -421,6 +423,34 @@
         scenario.onFragment { assertFalse(it.animationView!!.isAnimating) }
     }
 
+    @Test
+    fun testResumesWhenStateSavedAndRestored() {
+        class TestFragment : Fragment() {
+            override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+                return inflater.inflate(R.layout.auto_play, container, false)
+            }
+
+            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+                IdlingRegistry.getInstance().register(LottieIdlingResource(view.findViewById(R.id.animation_view)))
+            }
+        }
+
+        val scenario = launchFragmentInContainer<TestFragment>()
+        onView(withId(R.id.animation_view)).check(matches(isAnimating()))
+        scenario.onFragment { frag ->
+            val savedState = SparseArray<Parcelable>()
+            val animationView = frag.requireView().findViewById<LottieAnimationView>(R.id.animation_view)
+            val parent = animationView.parent as ViewGroup
+            val fragmentViewParent = frag.requireView().parent as ViewGroup
+            fragmentViewParent.removeView(parent)
+            parent.saveHierarchyState(savedState)
+            fragmentViewParent.addView(parent)
+            parent.restoreHierarchyState(savedState)
+
+        }
+        onView(withId(R.id.animation_view)).check(matches(isAnimating()))
+    }
+
     private fun FragmentScenario<*>.waitForState(desiredState: Lifecycle.State) {
         var isState = false
         while (!isState) {
diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
index 44b58bb..757a569 100644
--- a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
+++ b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
@@ -23,6 +23,7 @@
 import androidx.annotation.Nullable;
 import androidx.annotation.RawRes;
 import androidx.appcompat.widget.AppCompatImageView;
+import androidx.core.view.ViewCompat;
 
 import com.airbnb.lottie.model.KeyPath;
 import com.airbnb.lottie.parser.moshi.JsonReader;
@@ -247,7 +248,7 @@
     ss.animationName = animationName;
     ss.animationResId = animationResId;
     ss.progress = lottieDrawable.getProgress();
-    ss.isAnimating = lottieDrawable.isAnimating();
+    ss.isAnimating = lottieDrawable.isAnimating() || (!ViewCompat.isAttachedToWindow(this) && wasAnimatingWhenDetached);
     ss.imageAssetsFolder = lottieDrawable.getImageAssetsFolder();
     ss.repeatMode = lottieDrawable.getRepeatMode();
     ss.repeatCount = lottieDrawable.getRepeatCount();