Search code examples
androidandroid-5.0-lollipopshared-element-transitionactivity-transitionfragment-transitions

How to postpone a Fragment's enter transition in Android Lollipop?


In Android Lollipop, the Activity#postponeEnterTransition() and Activity#startPostponedEnterTransition() methods give the Activity the ability to delay starting the entering and exiting shared element transitions until all data is loaded. These work great for Activity transitions.

Is there a way to achieve the same effect when using Fragment transitions?


Solution

  • There's no direct equivalent in Fragment Transitions because Fragments use FragmentTransaction and we can't really postpone something that is supposed to happen in a transaction.

    To get the equivalent, you can add a Fragment and hide it in a transaction, then when the Fragment is ready, remove the old Fragment and show the new Fragment in a transaction.

    getFragmentManager().beginTransaction()
        .add(R.id.container, fragment2)
        .hide(fragment2)
        .commit();
    

    Later, when fragment2 is ready:

    getFragmentManager().beginTransaction()
        .addSharedElement(sharedElement, "name")
        .remove(fragment1)
        .show(fragment2)
        .commit();