I have the following situation inside of a soccer application.
We want to implement the shared elements between all these activities.
In my viewholder on th
Beyond any doubt, the problem is because you are changing transitionName
of the view that you want to share from second Activity
to third. But you should simply keep that transitionName
in the second Activity
but change transitionName
of the view in third Activity
's onCreate
method, according to what we want to share from second Activity
.
So let's keep our transition from first Activity
to second, as it's working as expected. Let's look at the second Activity
: we just need to send the transitionName
of view, that we want to share as an extra of Intent
to third Activity
and then assign this value programmatically to the shared view in third Activity
.
So here is the code of our second Activity
:
View homeTeam = findViewById(R.id.home_team_detail);
View awayTeam = findViewById(R.id.away_team_detail);
View.OnClickListener onTeamClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Activity activityContext = MultipleElementsDetail.this;
final ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(
activityContext,
Pair.create(v, v.getTransitionName()));
startActivity(new Intent(activityContext, SingleElementDetail.class)
.putExtra("shared_element_transition_name", v.getTransitionName()), options.toBundle());
}
};
homeTeam.setOnClickListener(onTeamClickListener);
awayTeam.setOnClickListener(onTeamClickListener);
So what I did here is just created the same OnClickListener
for both teams, which creates shared transition, and starts new activity with Intent
having transitionName
of shared view as an extra.
And then in third Activity
I've just get this extra from Intent
and set it as a transitionName
of shared view:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_element_detail);
View team = findViewById(R.id.team_single);
String transitionName = getIntent().getStringExtra("shared_element_transition_name");
if (!TextUtils.isEmpty(transitionName)) {
ViewCompat.setTransitionName(team, transitionName);
}
}
And as a result we have something like this (I've used explode transition to better see the difference between activities):
Hope that helps and exactly the same what you want! :)