Issue
I'm trying to use Android Navigation instead of fragment transaction. There is one problem however which is starting to be cumbersome. Upon using a slide-in animation for the Enter Animation
the new fragment goes beneath the current fragment. Be sure to check the following video to see the bug in action.
https://youtu.be/gFnXiEyiypM
The bug seems not to be from the Navigation
component though the hacky solutions (this and this) which have been introduced for this specific problem doesn't seem to fix it when Navigation
is used.
Isn't there a workaround for this until an official fix has been released?
Solution
There is a hacky fix to this problem until an official fix has been released by Google. The solution is to override onCreateAnimation
with this code:
class BaseFragment : Fragment() {
override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation? {
if (nextAnim == R.anim.fragment_enter) {
val nextAnimation = AnimationUtils.loadAnimation(context, nextAnim)
nextAnimation.setAnimationListener(object : Animation.AnimationListener {
private var startZ = 0f
override fun onAnimationStart(animation: Animation) {
view?.apply {
startZ = ViewCompat.getTranslationZ(this)
ViewCompat.setTranslationZ(this, 10f)
}
}
override fun onAnimationEnd(animation: Animation) {
// Short delay required to prevent flicker since other Fragment wasn't removed just yet.
view?.apply {
this.postDelayed({ ViewCompat.setTranslationZ(this, startZ) }, 100)
}
}
override fun onAnimationRepeat(animation: Animation) {}
})
return nextAnimation
} else {
return null
}
}
}
Answered By - 2hamed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.