Here\'s the scenario: Activity contains fragment A, which in turn uses getChildFragmentManager() to add fragments A1 and A2
I recently ran into this problem in my question: Nested fragments transitioning incorrectly
I have a solution that solves this without saving a bitmap, nor using reflection or any other unsatisfying methods.
An example project can be viewed here: https://github.com/zafrani/NestedFragmentTransitions
A GIF of the effect can be viewed here: https://imgur.com/94AvrW4
In my example there are 6 children fragments, split between two parent fragments. I'm able to achieve the transitions for enter, exit, pop and push without any problems. Configuration changes and back presses are also successfully handled.
The bulk of the solution is in my BaseFragment's (the fragment extended by my children and parent fragments) onCreateAnimator function which looks like this:
override fun onCreateAnimator(transit: Int, enter: Boolean, nextAnim: Int): Animator {
if (isConfigChange) {
resetStates()
return nothingAnim()
}
if (parentFragment is ParentFragment) {
if ((parentFragment as BaseFragment).isPopping) {
return nothingAnim()
}
}
if (parentFragment != null && parentFragment.isRemoving) {
return nothingAnim()
}
if (enter) {
if (isPopping) {
resetStates()
return pushAnim()
}
if (isSuppressing) {
resetStates()
return nothingAnim()
}
return enterAnim()
}
if (isPopping) {
resetStates()
return popAnim()
}
if (isSuppressing) {
resetStates()
return nothingAnim()
}
return exitAnim()
}
The activity and parent fragment are responsible for setting the states of these booleans. Its easier to view how and where from my example project.
I am not using support fragments in my example, but the same logic can be used with them and their onCreateAnimation function