onAnimationEnd() called twice

一个人想着一个人 提交于 2019-12-11 06:45:13

问题


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);

回答1:


Please try it

Before start animation

fragmentToPopView.clearAnimation();




回答2:


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

        @Override
        public void onAnimationEnd(Animation animation) {
            slideOutRightAnimation.setAnimationListener(null);
            // ... //
        }


来源:https://stackoverflow.com/questions/48354867/onanimationend-called-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!