Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when enter transition ended on Fragment?

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?

like image 673
Alex Wih Avatar asked Nov 24 '15 13:11

Alex Wih


People also ask

What is Fragment transition?

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.

How do you animate a fragment?

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).

What is transition in Android?

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.

What is a transition to a fragmenttransaction?

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:

Is there a callback for the enter transition for the fragment?

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?

How are shared element transitions applied to fragments?

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.

What is the difference between Enter/Exit transitions and shared element transitions?

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.


2 Answers

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);
like image 136
РусланT Avatar answered Oct 11 '22 06:10

РусланT


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().

like image 43
luckyhandler Avatar answered Oct 11 '22 04:10

luckyhandler