I'm sorry for posting a question on the subject again but this is driving me crazy.
I have a fragment and I'm adding views programmatically to a LinearLayout:
<LinearLayout
android:id="@+id/items_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
val items =buildView1(event) +buildView2(event) +buildCrew(event)
items.forEach {dataBinding.itemsContainer.addView(it)}
crew is a fragments, so I add it like this:
private fun buildCrew(event: Event): List<View> {
return listOf(
FrameLayout(requireContext()).apply {
id = View.generateViewId()
val transaction = childFragmentManager.beginTransaction()
val f = CrewFragment()
transaction.add(id, f)
transaction.commit()
})
}
it works but navigating away from this page and coming back results in the error:
No view found for id 0x1 (unknown) for fragment CrewFragment{92ba272}
I also tried to create a custom view with a FragmentContainerView:
<androidx.fragment.app.FragmentContainerView
android:id="@+id/event_detail_crew"
android:name="redacted.package.CrewFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
this solves the error but causes two problems:
the fragment seemed to be attached using the activity's fragment manager, I need the containing fragment's
childFragmentManageras the two need to share aviewModelwithprivate val viewModel: CrewViewModel by lazy { requireParentFragment().getViewModel() }
when navigating away and coming back the contents of
CrewFragmentare not visible anymore, none ofCrewFragment's lifecycle methods are called
What is going on here?