Android FragmentManager: Custom animation shows wrong exit fragment

109 Views Asked by At

I'm using the SupportFragmentManager to navigate between different fragments (let's call them A, B, C, D for simplicity). The navigation itself works correctly. The animation works only correctly considering the entering fragments. My code is basically the same for navigating between all fragments:

activity?.supportFragmentManager?.commit {
                    setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right)
                    replace(R.id.myNavHostFragment, BFragment())
                }

The navigation is only linear, so I navigate from A -> B, B -> C, and C -> D.

The problem is: only the animation of A -> B works correctly. For all others, during the animation, the wrong 'old' (exit) fragment A is shown in background, while the new one slides in.

So this means:

  • animation A -> B works fine.
  • animation B -> C does not work correctly. Fragment C slides in correctly. But while the animation plays, not Fragment B is shown in the background. Instead, somehow Fragment A is shown again.
  • the same for C -> D. Fragment A is shown, while C should be seen during the animation.

As you can see, I do not add fragments to the backstack. I really can't figure out why Fragment A is always shown during the animation, even if it should be a different one.

In a nutshell: the animation shows always the first fragment (A) as exit fragment, while it should be the previous one instead.

1

There are 1 best solutions below

0
On

Please check your navigation graph and re-visit all the below attributes shown in below image.
Open your own navigation_graph.xml and then click on Design tab. You will see below attribute(as shown in image) when you click on navigation direction.

Also, Animation has nothing to do with the back stack of your app.

You need to check Pop Behavior and Launch Options

Also please see if you have set any Flag's incorrectly in your code. For e.g. FLAG like FLAG_ACTIVITY_CLEAR_TOP

And finally please thoroughly go through this Article

Navigation Graph in Android Studio