问题
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