AzraelPwnz
AzraelPwnz

Reputation: 506

Remove fragment animation switch case

I am showing a child fragment from a fragment with this code:

getChildFragmentManager().beginTransaction()
            .setCustomAnimations(android.R.animator.fade_in, R.animator.slide_out)
            .replace(R.id.container, detailFragment, "MAP_DETAIL")
            .commit();

The goal is to only have the fragment execute the exit animation when a back button on the view is pressed.

Right now, the problem is that it is executing when I navigate to another view via a side-nav menu. This does not look good because the child fragment is popped off before the main fragment is replaced with the other view.

I want to remove the animation if the parent fragment is being replaced, but keep it when the user hits a back button (going from detail to main fragment)

I cant find anything online regarding this case, but if there is an answer I apologize.

Any help or guidance would be greatly appreciated.

(note, I do have a video to share that shows the child fragment exit animation being called when the parent fragment is replaced. Available if needed)

Upvotes: 0

Views: 54

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134714

For this you can use the setCustomAnimations method that takes 4 arguments:

  • enter animation
  • exit animation
  • pop-enter animation
  • pop-exit animation

The "enter" animation is shown as the Fragment appears, e.g. upon committing the transaction.

The "exit" animation is shown as the Fragment is replaced by another Fragment (i.e. a replace() transaction).

The "pop enter" animation is shown when the user pops the back stack, and this Fragment is returning to the foreground (i.e. there's a current foreground Fragment that is going away).

The "pop exit" animation is shown when the user pops the back stack, and this Fragment is leaving the foreground.

I believe the pop(enter/exit) animations should provide the behavior you need here.

Upvotes: 1

Related Questions