4

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?

2hamed
  • 8,719
  • 13
  • 69
  • 112

2 Answers2

2

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
        }
    }
}
2hamed
  • 8,719
  • 13
  • 69
  • 112
0

Unfortunately, this issue hasn't been resolved yet by Google. It has been reported here: https://issuetracker.google.com/issues/79443865

I struggle to understand how a company like Google is unable to provide something they tell us to do in their Material Guidelines.

Jeremi
  • 1,314
  • 3
  • 16
  • 40