android – Enter transition on a fragment with a shared element targets the shared element-ThrowExceptions

Exception or error:

I am using the new Lollipop api to setEnterTransition on a fragment and then add a shared element transition for an image in the fragment. The desired behavior is first, the image should move to its position, after which the rest of the views in the fragment should fade in.

However, the enterTransition is being applied to the shared element so it is fading in with the rest of the views. If I don’t set the enterTransition, then the image moves properly but it while it is moving the other content is already visible.

How do I get it to not apply the enterTransition to the shared view?

I found this commit in the AOSP that seems like it should address this issue, but it doesn’t seem to be working.

Here is sample code:

public class Fragment1 extends Fragment {

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main_fragment, container, false);
    final ImageView imageView = (ImageView) rootView.findViewById(;
    final Button button = (Button) rootView.findViewById(;

    button.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        TransitionSet transitionSet = new TransitionSet();
        transitionSet.addTransition(new ChangeImageTransform());
        transitionSet.addTransition(new ChangeBounds());

        Fragment fragment2 = new Fragment2();
        Fade fade = new Fade();

            .replace(, fragment2)
            .addSharedElement(imageView, "SharedImage")
    return rootView;
How to solve:

The enter transition should not apply the the shared element views. The most likely scenario is that your shared element is within another view with a background, making that view affected by the enter transition. That’s a situation like this:

    <ImageView android:src="@drawable/pretty_picture"

Here, the ImageView is the shared element.

If that happens, you’ll want to add a magic transition: ChangeTransform. If it detects that the parent changes, it will pull out the shared element from the parent and transition it separately.

Leave a Reply

Your email address will not be published. Required fields are marked *