androidandroid-fragmentsandroid-transitions

Fragment transition animation vs fragment view rendering


I couldnt find any answer for that, so here is the question.

When moving from Fragment A to Fragment B with some animation, is the animation performed after destination fragment is already rendered or is the animation perfomed during rendering of destination fragment ? So i.e. if my Fragment B needs like 2 sec to render completely (ignore device params) and I want to make sure thats its ready after transition animation is finished, then I can simply set animation duration to like 3 sec. How is it working actually ?


Solution

  • It happens at the same time and you can't guarantee which one comes first. But if you have some view or some data in the destination fragment that is need to be there before the transition, you can call Fragment.postponeEnterTransition() in the entering fragment's onViewCreated(). For example if you are navigating from fragment A to B:

    public class FragmentB extends Fragment {
    @Override
    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        ...
        postponeEnterTransition();
        }
    }
    

    Once you've loaded the data and are ready to start the transition, call Fragment.startPostponedEnterTransition().