3

I've followed instructions from dmanargias answer here: Android Fragments and animation

The animations themselves work, however the initial animation when adding a fragment is doing something strange. The initial fragment appears to be replaced with the new fragment before the animation is started.

e.g. One would expect an animation of A <- B (B sliding from right to cover A)

However as soon as the action starts A instantly becomes B and you get an animation of B <- B.

When popping the stack you get a correct animation of A -> B (B sliding away revealing A)

This is the code that adds a fragment:

CategoryFragment newFragment = CategoryFragment.newInstance();

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);

fragmentTransaction.replace(R.id.fragment, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Any ideas why this would happen and if there's a way to fix it?

Community
  • 1
  • 1
hyarion
  • 2,251
  • 2
  • 17
  • 28

2 Answers2

0

Try this:

    final CategoryFragment newFragment = CategoryFragment.newInstance();
    final View container = findViewById(R.id.fragment);
    container.postDelayed(new Runnable() {
        @Override
        public void run() {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
            transaction.replace(container.getId(), newFragment).commit();
            currentFragment = cardFragment;
        }
    }, 0);
Red Hot Chili Coder
  • 1,218
  • 1
  • 10
  • 19
0

I was having exactly the same problem, but with different animations. Check that android:shareInterpolator is not set to true in your xml animations.

Dmytro Karataiev
  • 1,214
  • 1
  • 14
  • 19