I am including a fragment in a row for a RecyclerView
- I know it is not the most recommended thing to do, but it is not used much and I would like it as a Fragment
as we are using it multiple places.
I have added this in the view for the ViewHolder
:
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/productRecommendationSectionFragment"
android:name="dk.xxx.ui.sections.productRecommendation.ProductRecommendationSectionFragment"
android:layout_width="match_parent"
android:tag="ProductRecommendationSectionFragment"
android:layout_height="wrap_content" />
It loads fine as expected but the question is then how to access the correct FragmentManager to configure the fragment?
The documentation states that it ".. add the Fragment to the appropriate FragmentManager"
and you should be able to access it by the tag.
But it does not seems to be either the contraining Fragment´ Fragment.fragmentManager
or Fragment.childFragmentManager
, nor the Activity.fragmentManager
. (Tested by a delegate to Containing Fragment)
Which could it then be?
One clue could be that in the ViewHolder calling binding.root.findFragment<ContainingFragment>()
resulting in IllegalStateException
:
"View androidx.fragment.app.FragmentContainerView... app:id/productRecommendationSectionFragment} does not have a Fragment set"
Edit:
I have tried those fragment managers from the containing fragment through a delegate.
activity!!.supportFragmentManager.findFragmentByTag("ProductRecommendationSectionFragment") // null
childFragmentManager.findFragmentByTag("ProductRecommendationSectionFragment") // null
fragmentManager.findFragmentByTag("ProductRecommendationSectionFragment") // null