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

前端 未结 3 645
囚心锁ツ
囚心锁ツ 2020-12-14 16:54

The new Shared Element Transitions works when i use Fragment \'replace\' but i can\'t seem to make it work fragment \'add\'. I use the same container in bo

相关标签:
3条回答
  • 2020-12-14 17:44

    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.

    0 讨论(0)
  • 2020-12-14 17:49

    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.

    0 讨论(0)
  • 2020-12-14 17:52

    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();
    
    0 讨论(0)
提交回复
热议问题