Skip to content Skip to sidebar Skip to footer

Multiple Shared Elements

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 the first Activity for th

Solution 1:

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 thirdActivity'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 secondActivity:

ViewhomeTeam= findViewById(R.id.home_team_detail);
ViewawayTeam= findViewById(R.id.away_team_detail);

View.OnClickListeneronTeamClickListener=newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        ActivityactivityContext= MultipleElementsDetail.this;
        finalActivityOptionsoptions= ActivityOptions.makeSceneTransitionAnimation(
                activityContext,
                Pair.create(v, v.getTransitionName()));
        startActivity(newIntent(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 thirdActivity I've just get this extra from Intent and set it as a transitionName of shared view:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_single_element_detail);

    Viewteam= findViewById(R.id.team_single);

    StringtransitionName= 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):

enter image description here

Hope that helps and exactly the same what you want! :)

Solution 2:

I had this doubt myself, but I feel like the answer above is a little bit confusing. To put it simply if you have more than one shared elements to animate, you can create "Pair" of View & transitionName as much as you want. Here is a sample code for this:

PairstatusAnim= Pair.create(holder.getOrderStatusView(), "track_job_status");
    PairdriverBundleAnim= Pair.create(holder.getDriverProfileBundle(), "driver_profile_bundle");
    ActivityOptionstransitionActivityOptions= ActivityOptions.makeSceneTransitionAnimation((Activity) context, statusAnim, driverBundleAnim);
    context.startActivity(newIntent(context, TrackingActivity.class), transitionActivityOptions.toBundle());

Post a Comment for "Multiple Shared Elements"