fragment to fragment animation with shared element don't work as expected

1k views Asked by At

In an AppCompatActivity with a RecyclerView in a fragment (myFragment1), viewholders and so on, so if I click on an item, then myFragment2 will show and display the detail of the item. In this phase, I want to create an animation between the two fragments with shared elements.
Steps to reproduce the behaviour:

import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
[...]
public class AppActivity extends AppCompatActivity

myFragment1.setSharedElementReturnTransition(new MyNewTransition());
myFragment1.setSharedElementEnterTransition(new MyNewTransition());
[...]
a) getSupportFragmentManager().beginTransaction()
                    .addSharedElement(myImage, "mytransition")
                    .replace(R.id.recycler_view_container, myFragment2)
                    .commit();

b) getSupportFragmentManager().beginTransaction()
                    .addSharedElement(myImage, "mytransition")
                    .add(R.id.recycler_view_container, myFragment2)
                    .hide(myFragment1) 
                     commit();

In the (a) code, the image transition is correct and animation occurs perfectly between the shared elements, but I don't need this. In the (b) code, the only difference is that myFragment1 is being hidden and the myFragment2 is added, in this case both the enter transition and the return transition are broken and no animation occurs.
I need the (b) code because "replace"-ing the fragments will destroy the myFragment1 and rebuilding it is a heavy-load process. I think this is a bug with androidx libraries

EDIT: I have build a sample based on the demonstration of Bryan Herbst:
FragmentTransitionSample and re-adapted for androidx, here are the sources.

1

There are 1 answers

2
Daniel Marantz On

Try adding setReorderingAllowed(true)

For example:

getSupportFragmentManager().beginTransaction() 
                .setReorderingAllowed(true)
                .addSharedElement(myImage, "mytransition") 
                .add(R.id.recycler_view_container, myFragment2) 
                .hide(myFragment1)  
                 commit();