Fragments - The specified child already has a parent. You must call removeView() on the child's parent first while fragment navigation

Viewed 178

I am getting this error and tried everything possible to get rid of this (removing animation solve my issue). So getting this error when am moving from one fragment to another but while transition i added entry/exit animation. When am on fragment A and moving to fragment B, instant back button is pressed in between animation and app get crash.

val navOptionBuilder = NavOptions.Builder().apply {
       setEnterAnim(R.anim.slide_in_right)
       setExitAnim(R.anim.slide_out_left)
      setPopEnterAnim(R.anim.slide_in_left)
      setPopExitAnim(R.anim.slide_out_right)
    } // *without animation it works fine* 
    findNavController().graph.addDestination(navDestination)
    findNavController().navigate(navDestination.id, bundle, navOptionBuilder.build())

crash message

Fragments - The specified child already has a parent. You must call removeView() on the child's parent first

buildToolsVersion '28.0.3'

1 Answers

I had the same issue because of animation as you and this code in onDestroyView method in my base Fragment fixed the crash

override fun onDestroyView() {
    if (rootView.parent != null){
      (rootView.parent as ViewGroup).endViewTransition(rootView)
    }
    super.onDestroyView()
}
Related