I called that flow of:

“Dynamic” Shared Element Transition

Coding to this was not a trivial task and I had to add some extra code to solve it.

🤔 How can I fixed that?

When you start an Activity using transitions like this:

The framework maps the shared elements and keep the mapping until you back to the previous Activity .

The transition has callbacks that help us to manage the transitions and they will be enough to cover general flows.

But there are some additional callbacks to deal with more advanced transitions.

With those callbacks, all you have to do is updating the Transition Shared Element on PreviewActivity every time you swipe, and pass some information to the previous GalleryActivity .

To cover the “Dynamic” Shared Element you need to take basically these steps:

Use the onActivityReenter callback in GalleryActivity

callback in Use the onMapSharedElements method of SharedElementCallback in PreviewActivity and GalleryActivity

🙇🏻 Digging into to the solution…

Implement the onActivityReenter callback and scroll to the current RecyclerView element position

The onActivityReenter callback is:

Called when an activity you launched with an activity transition exposes this Activity through a returning activity transition, giving you the resultCode and any additional data from it.

*This method will only be called if the activity set a result code other than RESULT_CANCELED and it supports activity transitions with FEATURE_ACTIVITY_TRANSITIONS .

Set that result when you are going to leave the PreviewActivity

You are already positioning the RecyclerView element correctly but the transition expected is not working.

So I present you SharedElementCallback :

Used in both Enter or Exit transition to monitor the Shared Element. The events can be used to customize Activity and Fragment Transition Behaviour.

For you flow, the most important method that you will need is:

Lets the SharedElementCallback adjust the mapping of shared element names to Views. names The names of all shared elements transferred from the calling Activity or Fragment in the order they were provided. sharedElements The mapping of shared element names to Views. The best guess will be filled into sharedElements based on the transitionNames.

You can use a SharedElementCallback instance and keep an updated reference to Shared Element Views that are current being shown on screen, for instance, when you swiping the ViewPager .

You can create a method like this:

When Transition API calls onMapSharedElements we will do two things:

Remove old references for View that is not a Shared Element anymore Add the new ones Shared Element View.

Setting our custom SharedElementCallback that I called MediaSharedElementCallback in PreviewActivity

And call sharedElementCallback.setSharedElementViews(imageView, checkbox) in setPrimaryItem method of our PagerAdapter

OK, you have the share element updated in PreviewActivity but the Transition API doesn’t match with the shared element in GalleryActivity .

You need to use our custom SharedElementCallback in GalleryActivity as well.

Set SharedElementCallback in GalleryActivity.onActivityReenter callback.

In this case I am using a Fragment, so I just delegate to GalleryFragment.onActivityReenter

💡 Tip:

Postpone the transition until RecyclerView 's onPreDraw event pass Find the ViewHolder Update the SharedElementCallback with the updated Shared Element View And then start the postponed transition.

We are already there. We still have some minor issue when we are starting a new transition after we have backed from Preview.

This issue happened because the Window Transition API use the last ExitSharedElementCallback that we set in onActivityReenter .

What we must do is:

Nulling that SharedElementCallback reference

A good time to do that is when the Exit Shared Element Transition is finished: