Shared element transitions with Navigation Components and FragmentContainerView

405 views Asked by At

I have a shared element transition between two fragments with worked perfectly well when using a NavHostFragment <fragment> as the container for my navigation graph fragments.

Nowadays it seems that instead of <fragment> one should use <androidx.fragment.app.FragmentContainerView> which supposedly brings many fixes, especially to how transitions are handled.

However, for me it breaks all my shared element transitions. I'm defining them like so (navigating from Fragment A to Fragment B):

fragment_a.xml

<ImageView
    android:id="@+id/logo"
    android:transitionName="logo" />

fragment_b.xml

<ImageView
    android:id="@+id/logo"
    android:transitionName="logo" />

FragmentA.kt

navController.navigate(
    FragmentADirections.actionFragmentAToFragmentB(),
    FragmentNavigatorExtras(logoView to "logo")
)

FragmentB.kt

override fun onCreate(savedInstanceState: Bundle?) {
    sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(R.transition.trans_logo_shared_element)
}

trans_logo_shared_element.xml

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="375"
    android:interpolator="@android:interpolator/fast_out_slow_in"
    android:transitionOrdering="together">
    <changeClipBounds />
    <changeTransform />
    <changeBounds />
</transitionSet>

The documentation of FragmentContainerView states that transitions are disabled for the container for Android L and up and that one should use FragmentTransaction.setCustomAnimations(int, int, int, int) instead. However I could not figure out how to get shared element transitions with Navigation Components to work with it.

0

There are 0 answers