I came across this same problem and this how I fixed it.
When the RecyclerView item is clicked, I pass the current position:
@Override
public void onItemClick(View sharedView, String transitionName, int position) {
viewPosition = position;
Intent intent = new Intent(this, TransitionActivity.class);
intent.putExtra("transition", transitionName);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(this, sharedView, transitionName);
ActivityCompat.startActivity(this, intent, options.toBundle());
}
I save it in onSaveInstanceState to persist across configuration changes:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(VIEW_POSITION, viewPosition);
}
Then, in the onCreate method:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Workaround for orientation change issue
if (savedInstanceState != null) {
viewPosition = savedInstanceState.getInt(VIEW_POSITION);
}
setExitSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
super.onMapSharedElements(names, sharedElements);
if (sharedElements.isEmpty()) {
View view = recyclerView.getLayoutManager().findViewByPosition(viewPosition);
if (view != null) {
sharedElements.put(names.get(0), view);
}
}
}
});
}
Cause of the problem: the sharedElements map was empty (I don't know why) after the orientation change.