FragmentTransaction.TRANSIT_NONE is not working as expected

164 Views Asked by At

How can I switch to another fragment without any animation?

I have two fragments and show/hide them using this:

supportFragmentManager
    .beginTransaction()
    .hide(currentFragment)
    .show(selectedFragment)
    .setTransition(FragmentTransaction.TRANSIT_NONE)
    .commit()

I expect them to switch without any animation, but there is a fade animation.

enter image description here

If I set the transition to TRANSIT_FRAGMENT_FADE, The fade animation will be shorter: enter image description here

I'm using androidx version 1.6.0-alpha02 and kotlin version 1.4.32

Thank you.

2

There are 2 best solutions below

0
Amir On BEST ANSWER

I found the problem.

It was because of Shared Element Transition that was setting in onCreate.

sharedElementEnterTransition = TransitionInflater.from(context).inflateTransition(android.R.transition.move)

Removing this line fixed the problem.

However I wonder in case of having a shared element transition, how can I navigate to another fragment without fading animation...

3
Shawn On

FragmentTransactions sets the animation for a fragment entering or exiting the transaction.

show(): displays a previously hidden fragment. This is only relevant for fragments that have been added to the container. Show will use the FragmentTransaction that was used when the Fragment originally entered the container.

hide(): Hides the existing fragment. This is only relevant for fragments whose views have been added to a container, as this will cause the view to be hidden. Hide will use the FragmentTransaction that was set when the fragment originally entered the trasaction.

In your case when you call FragmentTransaction, it isn't setting the animation because you don't have a fragment entering or exiting the container, you are using hide and show to display your fragment so the FragmentTransaction is using the animation for when the fragments had originally entered the container.

If you wish to control the animation I would suggest using replace instead of show and hide`.