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

纵然是瞬间 提交于 2019-11-30 04:41:56
ar34z

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.

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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!