I would like to add a shared elements transition using the navigation architecture components, when navigating to an other fragment. But I have no idea how. Also in the documentations there is nothing about it. Can someone help me?
They help users orient themselves by expressing your app's hierarchy, using movement... Navigation transitions use motion to guide users between two screens in your app. They help users orient themselves by expressing your app's hierarchy, using movement to indicate how elements are related to one another.
At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.
popUpTo and popUpToInclusive For example, if your app has an initial login flow, once a user has logged in, you should pop all of the login-related destinations off of the back stack so that the Back button doesn't take users back into the login flow.
The enterAnim and exitAnim is applied when navigating to or from the destination the "regular way", while popEnterAnim is applied to the destination when it is shown as a result of the destination "above" it being popped from the backstack.
FirstFragment
val extras = FragmentNavigatorExtras( imageView to "secondTransitionName") view.findNavController().navigate(R.id.confirmationAction, null, // Bundle of args null, // NavOptions extras)
first_fragment.xml
<ImageView android:id="@+id/imageView" android:transitionName="firstTransitionName" ... />
SecondFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { sharedElementEnterTransition = ChangeBounds().apply { duration = 750 } sharedElementReturnTransition= ChangeBounds().apply { duration = 750 } return inflater.inflate(R.layout.second_fragment, container, false) }
second_fragment.xml
<ImageView android:transitionName="secondTransitionName" ... />
I tested it. It is worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With