androidanimationanimatorset

Android AnimatorSet.cancel() does not work on Marshmallow version devices


AnimatorSet animatorSet = new AnimatorSet();
ValueAnimator anim1 = ValueAnimator.ofFloat(0f, 1f);
ValueAnimator anim2 = ValueAnimator.ofFloat(1f, 0f);
~~~
animatorSet.play(anim1).after(anim2);
animatorSet.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationCancel(Animator animation) {
        Log.d("Testing", "cancel");
    }
}

animatorSet.start();


button.setOnclickListener((v) -> {
    animatorSet.cancel();
})

When the Button is clicked, the cancel listener works well. However, cancel() is not called only in API23 version (Marshmallow). What's the problem?


Solution

  • Try to modify your cancel() method to this:

    public void cancel() {
      if (animatorSet != null) {
        animatorSet.cancel();
      }
      if (next != null) {
        next.cancel();
        next = null;
      }
    }
    

    It should work for API 23 also. If not, please share more code of your project to rebuild it and find another solution.