shared element transition works with FragmentTransaction.replace() but doesn't work with FragmentTransaction.add() shared element transition works with FragmentTransaction.replace() but doesn't work with FragmentTransaction.add() android android

shared element transition works with FragmentTransaction.replace() but doesn't work with FragmentTransaction.add()


I know this is an old question but recently i had the same problem: .replace was not an option.

I had to use .Hide and .Add and what worked for me was to set:

setReorderingAllowed(true) 

on the transaction.


I guess it is because the new fragment is placed on top of the old fragment. The old fragment is not being placed out of the controller and onPause (and sequenced methods) aren't being called. It doesn't play any transitions because the old fragment might still be visible to the user (the system doesn't know that).

In my answer (where you commented) I added an enter and exit transition. If you add it, does it even animate? If not, it's probably because of the stated reason.


It will not animate because the old fragment is still present. For adding you need to hide the previous fragment. Here is the example

For pushing:

    fragmentTransaction.add(R.id.content, fragment);    if (fragments.size() > 0) {        UIBaseFragment lastElement = fragments.lastElement();        fragmentTransaction.hide(lastElement);    }    fragmentTransaction.addToBackStack(null);    fragmentTransaction.commit();    fragments.push(fragment);

Then pop in this way

    final FragmentManager manager = getChildFragmentManager();    manager.popBackStack();    fragments.pop();