juanmeanwhile
juanmeanwhile

Reputation: 2634

Fragment enter and exit transitions are not executed at the same time

Running a simple slide to the left animation for both entering and existing fragment produces the effect of the entering fragment slightly overlapping with the exit fragment. This leads me to think that both transition are not executed at the same time. Any clue or confirmation of this behavior?

The desired effect is to slide the fragments to the left at the same time, without overlap.

The code:

Fragment current = ...;
Fragment fragment = ...;
Transition slideIn = TransitionInflater.from(this)
     .inflateTransition(R.transition.fragment_indicator_enter)
     .setDuration(300)
     .setInterpolator(new LinearInterpolator());
fragment.setEnterTransition(slideIn);

currentFragment.setExitTransition(TransitionInflater.from(this)
     .inflateTransition(R.transition.fragment_indicator_exit)
     .setDuration(300)
     .setInterpolator(new LinearInterpolator()));

getSupportFragmentManager()
     .beginTransaction()
     .replace(R.id.fragment_container, fragment)
     .addToBackStack(null)
     .commit();

The only workaround by know it has been to add a setStartDelay(30) for the entering transition. But weird thing, I have different transitions for different fragments and the startDelay has to be different to produce the effect of both fragment sliding to the left at the same time.

Upvotes: 3

Views: 6324

Answers (2)

juanmeanwhile
juanmeanwhile

Reputation: 2634

The effect is an expected behavior of the transition, as all the views in the layout are moved at a different time to avoid moving everything as a block a create some natural sense of motion. I intentionally want this block effect, so it's solved by adding a target for the transition, where this target is the FrameLayout containing the views of the fragment.

fragment.setEnterTransition(new Slide(Gravity.RIGHT)
                    .addTarget(R.id.whole_content));

Upvotes: 3

NSimon
NSimon

Reputation: 5287

Did you try placing the animations directly in the transaction call?

getSupportFragmentManager()
 .setCustomAnimations(R.transition.fragment_indicator_enter, R.transition.fragment_indicator_exit)
 .beginTransaction()
 .replace(R.id.fragment_container, fragment)
 .addToBackStack(null)
 .commit();

Upvotes: 0

Related Questions