I implement fragments as below:
The code sample looks like:
/*This code does not show animation*/
getChildFragmentManager()
.beginTransaction()
.addSharedElement(transitionView, ViewCompat.getTransitionName(transitionView))
.replace(R.id.fragment_container, categoryDetailChildFragment)
.addToBackStack(TAG)
.commit();
and the code that shows animation is:
getFragmentManager()
.beginTransaction()
.addSharedElement(transitionView, ViewCompat.getTransitionName(transitionView))
.replace(R.id.fragment_container, categoryDetailChildFragment)
.addToBackStack(TAG)
.commit();
Why there is no shared transition when getChildFragmentManager()? Please help anybody.
Every fragment has their own childFragmentManager. Therefore if you have multiple nested fragments one inside another you should refer to the same fragment's childFragmentManager, the same in which you use addSharedElement().
Therefore if you have:
ActivityA
|_ FragmentB
|_ FragmentC
|_ FragmentD
You should use the uppest common fragment's getChildFragmentManager() -Fragment B in this case, for Fragment C and Fragment D- to ensure every nested fragment refers to the same fragmentManager. That's why it works when you use activity's fragmentManager, because there are just one activity and every one refers to the same one by using getActivity()
To get parent's Fragment in a nested fragment you could use getParentFragment() on your nested fragment's onAttach() method. You could also cast it to a certain class like FragmentB:
override fun onAttach(context: Context) {
super.onAttach(context)
val fragment: FragmentB = parentFragment.parentFragment.parentFragment... as FragmentB
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With