androidandroid-fragmentsandroid-architecture-navigationandroid-viewpager2shared-element-transition

Shared element transition to a fragment that contains a ViewPager


I am trying to perform a shared element transition from a RecyclerView item in Fragment A to Fragment B. The transition-names are set on the outermost CardViews in both layouts. My implementation is basically the same as the one in this sample.

Everything worked fine until I added a ViewPager2 in Fragment B.

I tried to follow the steps in this answer, but to no luck.

Stack trace:

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)

Edit: Showing my dependencies in response to the answer suggesting that the Fragment version needs to be updated:

ext.kotlin_version = "1.5.10"
ext.navigation_version="2.4.0-alpha02"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

implementation 'androidx.preference:preference-ktx:1.1.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation "androidx.work:work-runtime-ktx:2.5.0"

// Navigation
implementation "androidx.navigation:navigation-runtime-ktx:$navigation_version"
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"

Solution

  • Adding a dependency on androidx.fragment:fragment-ktx:1.4.0-alpha03 fixed the issue. There is no longer a need to add empty views.

    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)