3

How to set animation when we are using addToBackStack() in fragment ?

My old fragment need to be stayed up and new fragment to should overlap the old fragment with animation and show it to user.

I tried setCustomAnimation(). But i can't.

Can anyone help me out ?

Sasikumar
  • 1,043
  • 1
  • 8
  • 9
  • Do you mean this? http://stackoverflow.com/questions/10886669/how-to-reverse-fragment-animations-on-backstack – ains Jun 30 '13 at 13:52

1 Answers1

1

When you are about to add the fragment you can use the FragmentTransaction class to set the animation.

So something like this...

FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

MyFragment fragment = new MyFragment();

    ft.add(android.R.id.content,fragment ,TAG);
    ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
    ft.addToBackStack(null);
    ft.commit();

This should lay your current fragment on top of anything else without removing anything. The "setTransition" animation will animate the fragment entrance but I would make an attempt to use "setCustomAnimations" to get full control of how the fragment will be animated.

Marco RS
  • 8,145
  • 3
  • 37
  • 45
  • `setTransition` doesn't accept value of `TRANSIT_ENTER_MASK`. From the docs: `Select a standard transition animation for this transaction. May be one of TRANSIT_NONE, TRANSIT_FRAGMENT_OPEN, TRANSIT_FRAGMENT_CLOSE, or TRANSIT_FRAGMENT_FADE.` ` – j2ko May 31 '17 at 12:08