LifecycleAwareAnimationController

A utility for keeping animations within the scope of a given lifecycleOwner.

This is useful in cases where the animation is only valid while the animating view is shown, and the animation end action should be performed immediately if the animating view stops being shown. A concrete example of where this is important, is in a system UI that may play an exit animation for a fragment before actually removing the fragment from the stack. If the animation is interrupted by a call to the containing activity's onSaveInstanceState, the fragment cannot be safely removed anymore by the time that the animation finishes.

Constructors

Functions

Link copied to clipboard

Ends all ongoing animations. This will cancel the animation and invoke the AnimationListener.onAnimationEnd on the animationListener.

Link copied to clipboard
fun runAnimation(view: View, animation: Animation, animationListener: Animation.AnimationListener? = null)

Runs animation on view, but only if the controller's lifecycle is in a started state. If the lifecycle is stopped, the animation will not be started and the animation end listener is called immediately.

fun runAnimation(view: View, @AnimRes animationResource: Int, onAnimationEnd: AnimationEndListener? = null)

Runs an animation created through animationResource on view, but only if the controller's lifecycle is in a started state. If the lifecycle is stopped, the animation will not be started and onAnimationEnd is called immediately.