Nested fragments disappear during transition animation

后端 未结 16 1720
广开言路
广开言路 2020-11-29 15:50

Here\'s the scenario: Activity contains fragment A, which in turn uses getChildFragmentManager() to add fragments A1 and A2

16条回答
  •  Happy的楠姐
    2020-11-29 16:07

    My problem was on parent fragment removal (ft.remove(fragment)), child animations were not happening.

    The basic problem is that child fragments are immediately DESTROYED PRIOR to the parents fragment exiting animation.

    Child fragments custom animations do not get executed on Parent Fragment removal

    As others have eluded to, hiding the PARENT (and not the child) prior to PARENT removal is the way to go.

                val ft = fragmentManager?.beginTransaction()
                ft?.setCustomAnimations(R.anim.enter_from_right,
                        R.anim.exit_to_right)
                if (parentFragment.isHidden()) {
                    ft?.show(vehicleModule)
                } else {
                    ft?.hide(vehicleModule)
                }
                ft?.commit()
    

    If you actually want to remove the parent you should probably set up a listener on you custom animation to know when the animation is ended, so then you can safely do some finalisation on the Parent Fragment (remove). If you don't do this, in a timely fashion, you could end up killing the animation. N.B animation is done on asynchronous queue of its own.

    BTW you don't need custom animations on the child fragment, as they will inherit the parent animations.

提交回复
热议问题