Activity and Fragment Transitions in Lollipop

8.2k views Asked by At

I'm trying to wrap my head around the new Activity Transition framework in Lollipop. The Activity Transition works pretty straighforward and there are some basic info here, but the Fragment Transition is undocumented and I can't get it to work. I've tried this use case (very common in Android):

case 1: ActA+FragA -> ActB+FragB

with the sharedElement being an image in FragA and FragB. I didn't come up with working code, so I went a step back and tried

case 2: ActA+FragA -> ActB

with a sharedElement on FragA and ActB. The animation won't work, I can only see that when I click the image on FragA, the image disappear and after the animation's duration it pops up in ActB. Shared views outside FragA but inside ActA (the Toolbar for example) animate correctly.

In this case the sharedImage is an imageView in a RecyclerView, could it be that the xml tag android:transitionName="shared_icon" in the item's layout xml doesn't work?

This is my Theme:

 <!-- Window Transactions -->
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:fragmentAllowEnterTransitionOverlap">@bool/true_bool</item>
    <item name="android:fragmentAllowReturnTransitionOverlap">@bool/true_bool</item>

    <item name="android:windowEnterTransition">@transition/window_transition.xml</item>
    <item name="android:windowExitTransition">@transition/window_transition.xml</item>
    <item name="android:fragmentEnterTransition">@transition/window_transition.xml</item>
    <item name="android:fragmentReturnTransition">@transition/window_transition.xml</item>
    <item name="android:fragmentReenterTransition">@transition/window_transition.xml</item>

    <!-- Shared Element Transactions -->
    <item name="android:windowSharedElementEnterTransition">@transition/shared_elements_transform.xml</item>
    <item name="android:windowSharedElementExitTransition">@transition/shared_elements_transform.xml</item>

    <item name="android:fragmentSharedElementEnterTransition">@transition/shared_elements_transform.xml</item>
    <item name="android:fragmentSharedElementReturnTransition">@transition/shared_elements_transform.xml</item>

window_transition.xml:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together"
android:duration="@integer/act_transition_duration">
<changeBounds  />
<changeTransform />
<changeClipBounds />
<changeImageTransform />
</transitionSet>

shared_element_transition.xml:

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together"
android:duration="@integer/act_transition_duration">
<changeImageTransform />
<changeBounds />
</transitionSet>
1

There are 1 answers

1
George Mount On

Fragment Transitions are meant to work between Fragments in the same Activity. If you have any two different Activities, whether they have fragments or not, you are using Activity Transitions. Feel free to ignore all the Fragment Transition properties.

In your case 2, you should have no problems with your transitions if it is set up properly. I'm guessing that your application theme does not derive from android:Theme.Material, so you need one more property:

<item name="android:windowActivityTransitions">true</item>

windowContentTransitions allows you to use a TransitionManager to smoothly animate between setContentView of your window.

When you have a fragment in your launched Activity, like case 1, you may need to do as @AlexLockwood suggested: postponeEnterTransition. However, you should also be able to use:

getFragmentManager().executePendingTransactions();

inside your onCreate() to force the fragment to load right away so that the Activity Transition will see all the views in your layout.