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?
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