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>
Activity is the part where the user will interacts with your application. In other words, it is responsible for creating a window to hold your UI components. (UI components and how to build a layout will be discussed in another article). Fragment represents a behavior or a portion of user interface in an Activity.
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.
At runtime, a FragmentManager can add, remove, replace, and perform other actions with fragments in response to user interaction. Each set of fragment changes that you commit is called a transaction, and you can specify what to do inside the transaction using the APIs provided by the FragmentTransaction class.
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.
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