0

I've seen lots of people describing this happening with child fragments (for example), but I'm not using child fragments. When I add animations to the transaction, the exiting fragment disappears instantly while the entering fragment enters with the expected enter animation. When I press back, the same thing happens...the exiting fragment disappears instantly and the original fragment enters with the appropriate pop enter animation.

What have I done wrong?

I'm doing this in an AppCompatActivity that implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback, and my layout for the activity consists solely of a FrameLayout.

override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
    val fragment = supportFragmentManager.fragmentFactory.instantiate(
            classLoader,
            pref.fragment)
    supportFragmentManager.beginTransaction()
            .setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, R.anim.slide_in_left, R.anim.slide_out_right)
            .add(R.id.fragment_wrapper, fragment)
            .addToBackStack(null)
            .commit()
    return true
}

All of my animations are simple translate anims with the same duration, like this:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromXDelta="100%p" android:toXDelta="0"
    android:duration="@integer/fragment_transition_time"/>

I'm using androidx.preference:1.1.0-alpha05.

Tenfour04
  • 83,111
  • 11
  • 94
  • 154

1 Answers1

0

Use a ObjectAnimator (docs) instead of a TranslateAnimation.

Raimo
  • 1,494
  • 1
  • 10
  • 19
  • Is there a documented reason why what I'm doing is wrong? Can an ObjectAnimator be set up from XML to use 100% of the width of the fragment in the layout? – Tenfour04 Jul 04 '19 at 20:28
  • Not really, some people say TranslateAnimation is deprecated but that's not what the docs say. Just animate the "x" property to slide in and out and use float valuetype. – Raimo Jul 04 '19 at 20:47