Shared Element Transition from fragment to fragment on new activity

3k views Asked by At

I'm working on an app that currently uses a content transition on an ImageView from one fragment to another on the same activity. It is working fine however I have realised that my destination fragment needs to have it's own activity.

So let's say i have Activity A which contains Fragment 1 And I have Activity B which contains Fragment 2. I need to perform a shared element transition from Fragment 1 to Fragment 2.

Here is what i have done so far: In the callback method from Fragment 1 to Activity A I'm passing the selected entity and also the imageview i want to transition from.

Activity A

 @Override
public void OnPhotographSelected(Photograph selectedPhoto,ImageView image) {
    Intent i= new Intent(this, PhotoDetailActivity.class);
    i.putExtra("photo_OBJ", selectedPhoto);
    i.putExtra("transitionName", image.getTransitionName());
    startActivity(i, ActivityOptions.makeSceneTransitionAnimation(this, image, "mainPhoto").toBundle());
}

Activity B

 @Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_detail);
    Photograph photoObj=new Photograph();
    Bundle b = getIntent().getExtras();
    String transitionName="";
    if(b!=null)
    {
        photoObj=(Photograph)b.getSerializable("photo_OBJ");
        transitionName=b.getString("transitionName");
    }
    PhotoDetailFragment pdf = PhotoDetailFragment.newInstance(photoObj);
    pdf.setSharedElementReturnTransition(TransitionInflater.from(this).inflateTransition(R.transition.change_image_transform));
    pdf.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.change_image_transform));
    pdf.setImageTransitionId(transitionName);
    FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
    trans.replace(R.id.photo_detail_content, pdf);
    trans.commit();
}

Fragment 2

 mainImg.setTransitionName(mImageTransitionID);

Activity Theme

 <item name="android:windowActivityTransitions">true</item>

I'm not seeing any content transition occur at runtime. As i mentioned earlier I had this transition working correctly from fragment to fragment within the same activity. Also worth noting is that Fragment 1 is a gridview so i have to maintain the transitionNames myself so they are all unique, that why you are seeing setTransitionName calls at runtime.

Any idea's why i'm not see the transition run?

1

There are 1 answers

0
Devofure On

Try to use postponeEnterTransition() in your second activity inside onCreate() and yourActivity.startPostponedEnterTransition() in your fragment after you created your view in onViewCreated().

If you're using AppCompat try supportPostponeEnterTransition() and supportStartPostponedEnterTransition() or ActivityCompat.postponeEnterTransition(yourActivity) and ActivityCompat.startPostponedEnterTransition(yourActivity).

Credits to: http://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html