I have a fragment, which shows enter animation, I set transition by
this.setEnterTransition(transition);
After that I want to show another animation. But I need to know when transition animation ends to start the second one.
For activity there is a callback like onEnterAnimationComplete()
but it is not called when Fragment's transition ends.
Is there any way to know when enter transition ends for the Fragment?
The Fragment API provides two ways to use motion effects and transformations to visually connect fragments during navigation. One of these is the Animation Framework, which uses both Animation and Animator . The other is the Transition Framework, which includes shared element transitions.
To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).
Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.
This Fragment Transaction -by default- will simply replace one Fragment with the other, by applying a Transition to this FragmentTransaction we can make our application gracefully animate this change like this:
For activity there is a callback like onEnterAnimationComplete () but it is not called when Fragment's transition ends. Is there any way to know when enter transition ends for the Fragment?
Shared Element Transitions are applied to the Fragment Transaction. If we look at our preview of the Transition (beginning of the article) we will see that 3 different animations are chained. · First, the text fades out. · Next, the logo moves (and automatically resizes). In order for this to work we will have to time our animations correctly.
It is important to understand that Enter/Exit Transitions are applied to the Fragments themselves. Shared Elements are elements that exist in both Fragments involved in the Transaction. Shared Element Transitions are applied to the Fragment Transaction.
transition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {}
@Override
public void onTransitionEnd(Transition transition) {}
@Override
public void onTransitionCancel(Transition transition) {}
@Override
public void onTransitionPause(Transition transition) {}
@Override
public void onTransitionResume(Transition transition) {}
});
this.setEnterTransition(transition);
If you have the following setup:
FragmentA calls FragmentB with a SharedElementEnterTransition like
private final TransitionSet transition = new TransitionSet()
.addTransition(new ChangeBounds());
//...
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment, fragment.getClass().getSimpleName());
transaction.addSharedElement(view, view.getTransitionName());
fragment.setSharedElementEnterTransition(transition);
fragment.setSharedElementReturnTransition(transition);
transaction.commit();
to listen for the end of the SharedElementTransition in your second Fragment. Then you have to get the SharedElementEnterTransition in your FragmentB's onAttach like:
@Override
public void onAttach(Context context) {
super.onAttach(context);
TransitionSet transitionSet = (TransitionSet) getSharedElementEnterTransition();
if (transitionSet != null) {
transitionSet.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(@NonNull Transition transition) {
// remove listener as otherwise there are side-effects
transition.removeListener(this);
// do something here
}
@Override
public void onTransitionStart(@NonNull Transition transition) {}
@Override
public void onTransitionCancel(@NonNull Transition transition) {}
@Override
public void onTransitionPause(@NonNull Transition transition) {}
@Override
public void onTransitionResume(@NonNull Transition transition) {}
});
}
}
As pointed out in the comments of this answer there is a bug when not setting the listener in onAttach().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With