Child fragment of BottomsheetDialogFragment crashes after second attempt opening

1

I have a fragment which is a child of BottomsheetDialogFragment, inside that fragment I have a google map. At first when I open this fragment using arch components NavController it works fine, than if I close this fragment and than try to open it again it crashes. This is the error log

Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #71 in com.example.com:layout/product_location_fragment: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #71: Duplicate id 0xffffffff, tag mapFragment, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:116)

Here is the fragment itself

  class ProductLocationFargment : BottomSheetDialogFragment, {


  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View? {
    return inflater.inflate(R.layout.product_location_fragment, container, false)
  }

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    //this also always returns null
    val frag = childFragmentManager.findFragmentById(R.id.mapFragment) as? SupportMapFragment

  }

}

and layout xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      <fragment
        android:tag="mapFragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        app:layout_constraintStart_toStartOf="@id/leftGuideView"
        app:layout_constraintEnd_toEndOf="@id/rightGuideView" />


    </androidx.constraintlayout.widget.ConstraintLayout>

This is how I'm starting that fragment from another fragment

button.setOnClickListener {
  val direction = AnotherFragmentDirections.actionAnotherFragmentToProductLocationFargment()
  navController.navigate(direction)
}
android
android-dialogfragment
android-architecture-components
android-architecture-navigation
android-bottomsheetdialog
asked on Stack Overflow Jan 29, 2020 by Jemo Mgebrishvili • edited Jan 29, 2020 by Jemo Mgebrishvili

1 Answer

1

Try adding the mapFragment to a FrameLayout manually instead of with <fragment tag, but only if if(savedInstanceState == null) { otherwise get it by tag

answered on Stack Overflow Jan 29, 2020 by EpicPandaForce

User contributions licensed under CC BY-SA 3.0