10

I have a shared element in a fragment that belongs to one Activity.

I want to make a shared element transition in Android Lollipop with an element that is part of a fragment that belongs to another activity.

Is it possible?

How can I achieve that?

Matroska
  • 6,885
  • 14
  • 63
  • 99

2 Answers2

10

It's possible.

First, when you detect in your fragment that transition is about to happen, build a array of Pair<View, String> which you populate with view and transition name.

For example, if you want to animate from thumbnail image to full width image:

  Pair[] pairs = new Pair[1];
  pairs[0] = new Pair(thumbnailImage, "THUMBNAIL_IMAGE");

Second, pass that array to fragment's activity so it can initiate the actual transition. (I'm using Otto to pass that event up, you can use usual callbacks if you like).

Then, in your activity, start the second activity. (I created a simple method for doing that)

public static void transitionExpand(Activity activity, Intent intent, Pair<View, String>[] sharedElements) {
        ActivityOptionsCompat options =
            ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElements);

        ActivityCompat.startActivity(activity, intent, options.toBundle());
}

In your second activity, you can add the fragment in the usual way. Then, in second fragment's onViewCreated() method, you can call:

ViewCompat.setTransitionName(fullWidthImage, "THUMBNAIL_IMAGE");

hope it helps

Tomislav Novoselec
  • 4,570
  • 2
  • 27
  • 22
  • It is not working...should I use also addSharedElement (View sharedElement, String name)? In the called activity, I have a fragment with a pager and a fragment in it. Could it be the problem? – Matroska Dec 17 '14 at 14:33
  • I am trying now with an easier part without the pager involved. If it doesn't work, I will share the code. Thanks! – Matroska Dec 17 '14 at 14:41
  • 1
    Oh, your fragment is embedded inside of ViewPager? I ran into issues with that also and I couldn't make it work. – Tomislav Novoselec Dec 17 '14 at 14:42
  • 3
    Setting android:transitionName="THUMBNAIL_IMAGE" on the shared view in the second fragment's layout also works. I've also found this solution only works for fragments statically declared in the parent's XML, not dynamically added fragments. – user1405990 Jan 19 '15 at 04:22
  • Works also with dynamically added fragments - thank you! – Katharina Mar 28 '17 at 14:48
  • Its is not working when return from FragmentB in Activity B to Fragment A in Activity A. @Katharina can you confirm that? – extmkv Jun 09 '17 at 17:15
  • 1
    @extmkv as far as I remember the animation worked in both directions – Katharina Jun 14 '17 at 12:22
  • 1
    I found the problem @Katharina ! The transaction should be made before activity::onStart – extmkv Jun 20 '17 at 09:20
2

UPDATE: As of v25.1.1 of the support library, these same methods are in the support Fragments. Links to the docs: Fragment.postponeEnterTransition() and Fragment.startPostponedEnterTransition()

ORIGINAL ANSWER:

It's possible, even with a dynamically added Fragment in the second Activity.

You just need to tell the second Activity not to run its Transition animations until the shared elements have been laid out and measured.

In the onCreate of the second Activity call postponeEnterTransition() (or supportPostponeEnterTransition() if you're using the Support Library). Dynamically add your Fragment to this Activity. At the end of the onCreateView method in the Fragment that you're dynamically adding, call getActivity().startPostponedEnterTransition().

This of course assumes you've done everything else required for a shared element transition, but I believe these methods are what you're searching for with your question.

Credit to @alex-lockwood's blog for showing me the light.

pumpkinpie65
  • 960
  • 2
  • 14
  • 16