How do I know when a transition ends in a fragment?

I have a snippet that shows the input animation, I set the transition to

this.setEnterTransition(transition);

After that, I want to show another animation. But I need to know when the transition animation ends to start the second.

There is a callback for activity, for example onEnterAnimationComplete(), but it is not called when the fragment transition ends.

Is there any way to know when the transition ends for a fragment?

+4
source share
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);
+3
source

If you have the following setting:

FragmentA calls FragmentB with SharedElementEnterTransition, e.g.

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

SharedElementTransition . SharedElementEnterTransition FragmentB onAttach, :

@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) {}
        });
    }
}

, , onAttach().

+1

Source: https://habr.com/ru/post/1617248/


All Articles