onAnimationEnd() called twice

2019-07-31 17:08发布

After updating the build sdk 27 from 23 came across this issue of onAnimationEnd firing twice in the code below when called. onAnimationStart is called only once and onAnimationRepeat is not called as expected. Now in the app when the user presses the back button one time, they are taken two steps back.

All the libraries in gradle are using 27.0.2 which is the latest. This code used to work fine in sdk 23. Our min target is 16.

I'm using a work around by using a isAnimating flag but would like to find the underlying cause which could be affecting other areas of the app.

@Override
public void onBackPressed() {
        Animation slideOutRightAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_out_right);
        slideOutRightAnimation.setFillAfter(true);
        slideOutRightAnimation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isAnimating = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if(isAnimating) { // Fix
                    getSupportFragmentManager().popBackStackImmediate();
                }
                isAnimating = false;
            }
        });
        fragmentToPopView.clearAnimation();
        fragmentToPopView.startAnimation(slideOutRightAnimation);

2条回答
孤傲高冷的网名
2楼-- · 2019-07-31 17:14

Removing the old listener on the onAnimationEnd will solve your problem.

        @Override
        public void onAnimationEnd(Animation animation) {
            slideOutRightAnimation.setAnimationListener(null);
            // ... //
        }
查看更多
霸刀☆藐视天下
3楼-- · 2019-07-31 17:20

Please try it

Before start animation

fragmentToPopView.clearAnimation();

查看更多
登录 后发表回答