'Android navigation controller navigations are overriden

I am currently building an application. I am using a single activity and multiple fragments to represent different layouts and functionalities. I am using Navigation Controller and the animations are not consistent. I have 3 fragments, let's say A, B and C. I can go to C from both A and B. B actually provides more information about fragment C and i have implemented a shared element material container transform.

  • When going from A -> C and C->A the animations work perfect
  • When going from A->B and B->A again, the material container transform works great
  • If I go from A->B, B->A , A->C and back to A, all the animations from A->C and back seem to be reseted and will not change unless i restart the fragment A by going to another fragment ( using bottom navigation bar)

My navigation graph xml is:

<fragment
    android:id="@+id/A"
    tools:layout="@layout/A">
        <action
        android:id="@+id/action_A_to_C"
        app:destination="@id/C"
        app:enterAnim="@anim/slide_in"
        app:exitAnim="@anim/fade_out"
        app:popEnterAnim="@anim/fade_in"
        app:popExitAnim="@anim/slide_out" />

    <action
        android:id="@+id/action_A_to_B"
        app:destination="@id/B"/>

</fragment>

<fragment
    android:id="@+id/B"
    tools:layout="@layout/B">
    <action
        android:id="@+id/action_B_to_C"
        app:destination="@id/C" />
    <action
        android:id="@+id/action_B_to_A"
        app:destination="@id/A" />
</fragment>

<fragment
    android:id="@+id/C"
    tools:layout="@layout/C">
    <action
        android:id="@+id/action_C_to_A"
        app:destination="@id/A" />
</fragment>

FragmentA.kt

//navigate A->B with material transform//
            val extras = FragmentNavigatorExtras(binding.handCard to "transition_hand_card")
            val directions = A_FragmentDirections.actionA_FragmentToB_Fragment()
            findNavController().navigate(directions, extras)

            exitTransition = MaterialElevationScale(false).apply {
                duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
            reenterTransition = MaterialElevationScale(true).apply {
                duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
}
//navigate A->C
            val directions = Α_FragmentDirections.actionA_ToC_()
            findNavController().navigate(directions,animationOptions)

FragmentB.kt

//navigate B->C
button.setOnClickListener{
            val directions = B_FragmentDirections.actionB_FragmentToC_Fragment()
            findNavController().navigate(directions , animationOptions)
}
    override fun onCreate(savedInstanceState: Bundle?)  {
        super.onCreate(savedInstanceState)

        sharedElementEnterTransition = MaterialContainerTransform().apply {
            drawingViewId = R.id.nav_host_fragment_activity_main
            duration = resources.getInteger(R.integer.reply_motion_duration_large).toLong()
            scrimColor = Color.TRANSPARENT
            setAllContainerColors(requireContext().themeColor(R.attr.colorSurface))
        }
    }

To reiterate, if i open fragment B , the animations going to fragment C and leaving from fragment C are being reset to defaults? How to fix this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source