Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use shared element transitions in Navigation Controller

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?

like image 827
JPLauber Avatar asked May 30 '18 08:05

JPLauber


People also ask

Is there a need for transitions in navigation?

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.

How do you animate fragment transitions?

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.

What is popUpToInclusive?

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.

What is popEnterAnim?

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.


1 Answers

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.

like image 132
Xzin Avatar answered Oct 11 '22 11:10

Xzin