Search code examples
androidandroid-transitionsshared-element-transitionfragment-transitions

Shared element transition - number of views


Does a shared element transition from one view to another need to contain the same amount of child views? My experience is that the following crash appears if this is not the case:

java.lang.IndexOutOfBoundsException: Index: 5, Size: 5
        at java.util.ArrayList.get(ArrayList.java:437)
        at androidx.fragment.app.FragmentTransitionImpl.setNameOverridesReordered(FragmentTransitionImpl.java:182)
        at androidx.fragment.app.DefaultSpecialEffectsController.startTransitions(DefaultSpecialEffectsController.java:665)
        at androidx.fragment.app.DefaultSpecialEffectsController.executeOperations(DefaultSpecialEffectsController.java:114)
        at androidx.fragment.app.SpecialEffectsController.executePendingOperations(SpecialEffectsController.java:294)
        at androidx.fragment.app.Fragment$3.run(Fragment.java:2776)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Adding empty views usually resolves the issue. I could not find anything in the documentation about this, hence the question.


Solution

  • The views do not need to contain the same amount of child views.

    This was a bug in Fragment version 1.3.4 and it is fixed now.

    Adding a dependency on androidx.fragment:fragment-ktx:1.4.0-alpha03 resolved the issue.

    The release notes states the following:

    From Fragment 1.3.5: Fixed a regression in shared element transitions introduced in Fragment 1.3.4 by aosp/1679887. Fragments now correctly handle transition groups (either set directly via transitionGroup="true" or indirectly via a transitionName or background) and shared elements will no longer throw IndexOutOfBoundsExceptions. (I16484, b/188679569, b/188969304)