(#) Creating an Animatable during composition without using `remember`
!!! ERROR: Creating an Animatable during composition without using `remember`
This is an error.
Id
: `UnrememberedAnimatable`
Summary
: Creating an Animatable during composition without using `remember`
Severity
: Error
Category
: Correctness
Platform
: Any
Vendor
: Jetpack Compose
Identifier
: androidx.compose.animation.core
Feedback
: https://issuetracker.google.com/issues/new?component=612128
Min
: Lint 8.0 and 8.1
Compiled
: Lint 8.7+
Artifact
: [androidx.compose.animation:animation-core-android](androidx_compose_animation_animation-core-android.md.html)
Since
: 1.5.0
Affects
: Kotlin and Java files and test sources
Editing
: This check runs on the fly in the IDE editor
Implementation
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/animation/animation-core-lint/src/main/java/androidx/compose/animation/core/lint/UnrememberedAnimatableDetector.kt)
Tests
: [Source Code](https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:/compose/animation/animation-core-lint/src/test/java/androidx/compose/animation/core/lint/UnrememberedAnimatableDetectorTest.kt)
Copyright Year
: 2021
Animatable instances created during composition need to be `remember`ed,
otherwise they will be recreated during recomposition, and lose their
state. Either hoist the Animatable to an object that is not created
during composition, or wrap the Animatable in a call to `remember`.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/{.kt:12:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:13:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:15:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:20:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:21:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:23:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:28:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:29:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:31:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:41:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:42:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:44:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:48:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:49:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:51:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:58:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:59:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:61:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:66:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:67:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:69:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
src/test/{.kt:77:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable = Animatable<Boolean, Any>(false)
----------
src/test/{.kt:78:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable2 = Animatable(0f)
----------
src/test/{.kt:80:Error: Creating an Animatable during composition
without using remember [UnrememberedAnimatable]
val animatable3 = Animatable(Color.Red)
----------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the source file referenced above:
`src/test/{.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test
import androidx.compose.animation.*
import androidx.compose.animation.core.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.*
@Composable
fun Test() {
// Float function and constructor from androidx.compose.animation.core
val animatable = Animatable