I want to apply animation when I press back button from fragment.
For this, I know that I can use overridePendingTransition like below.
override fun finish() {
super.finish()
overridePendingTransition (0, R.anim.slide_down_enter)
}
Then I can apply animation before closing this fragment.
However this works for all fragment (or activity).
I want to apply only one View of this fragment, but overridePendingTransition doesn’t provides such function.
I tried to give pending like below in onBackPressed.
override fun onBackPressed() {
super.onBackPressed()
resumePause = true
var downAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_down_enter)
binding.commentLayout.startAnimation(downAnimation)
Handler().postDelayed({
finish()
}, 500)
}
But It also doesn’t work.
Is there any way to do this?
>Solution :
Hey you can override the onBackPressed method in your activity and apply the animation to the view you want to animate.
Here’s an example:
override fun onBackPressed() {
val downAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_down_enter)
binding.commentLayout.startAnimation(downAnimation)
downAnimation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(animation: Animation?) {}
override fun onAnimationEnd(animation: Animation?) {
finish()
overridePendingTransition(0, R.anim.slide_down_enter)
}
override fun onAnimationRepeat(animation: Animation?) {}
})
}