Google has just released android.support.transition package which provides backport to its transition-framework, which is really awesome.
https://developer.android.com/reference/android/support/transition/package-summary.html
Unfortunately, when trying to use the Transition/TransitionSet object from this package as a shared element for fragment transaction, the application fails on following error :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: gregor.martin.loginregisteractivity, PID: 26186
java.lang.ClassCastException: android.support.transition.TransitionSet cannot be cast to android.transition.Transition
at android.support.v4.app.FragmentTransitionCompat21.wrapSharedElementTransition(FragmentTransitionCompat21.java:86)
at android.support.v4.app.BackStackRecord.getSharedElementTransition(BackStackRecord.java:1156)
at android.support.v4.app.BackStackRecord.configureTransitions(BackStackRecord.java:1222)
at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1112)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:721)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Is there any way to use the new backported transition as a shared element transition?
Android also supports these shared elements transitions: changeBounds - Animates the changes in layout bounds of target views. changeClipBounds - Animates the changes in clip bounds of target views. changeTransform - Animates the changes in scale and rotation of target views.
To animate the transition between fragments, or to animate the process of showing or hiding a fragment you use the Fragment Manager to create a Fragment Transaction . Within each Fragment Transaction you can specify in and out animations that will be used for show and hide respectively (or both when replace is used).
At a high level, here's how to make a fragment transition with shared elements: Assign a unique transition name to each shared element view. Add shared element views and transition names to the FragmentTransaction . Set a shared element transition animation.
Shared Element Transition in Android determines how shared element views are animated from activity to activity or fragment to fragment. Now we will see the implementation of Shared Element Transition in our app with a simple example.
This transition effect is available only on devices running on Lollipop (Android 5.0 – API level 21) and higher. Shared elements transitions were introduced in Android 5.0 to make view transitions across screens more seamless and easy to implement. Using this transition, the switch between Activities or Fragments seems more natural and unforced.
Be careful with shared element transitions. You do not want too many shared elements, which can become distracting and confusing rather than smooth and natural. You want to direct user focus in a non obtrusive manner. Shared element transitions with Fragments works in an idealistically similar way to Activities shown above.
To animate the transition in a FragmentTransaction, we call setSharedElementEnterTransition () and setEnterTransition () on the target fragment (EndFragment). We also call setSharedElementReturnTransaction () and setExitTransition () on the start fragment (StartFragment).
UPDATE (2017-08-31)
It seems like that the support Fragment API will be able to use the support Transition API in the upcoming release of the support library (see the latest comments by Ian Lake in the same topic linked in the original answer). Whether it means that the transitions will be backported to pre-Lollipop devices or just a simple solution for the casting problems, there's no info about it, but I will update the answer once I know more. I'll try to look into the source before the release when I'll have some time.
Original answer:
This support transition API is for views only (for the time being, at least), thus you cannot use it on fragments. What a shame...
Confirmed by Ian Lake in this Google+ answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With