I have the need to "stack up" fragments one on top of another. I do this by:
String className = fragment.getClass().getName();
FragmentManager fragmentManager = getChildFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_content, fragment, className);
fragmentTransaction.addToBackStack(className);
fragmentTransaction.commit();
Within:
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
This all works good as long as the fragments are of different type.
However if I create a second instance of a fragment type that is already there then there is an exception:
java.lang.IllegalStateException: Fragment already added: TestFragment{1184e8a1 #0 id=0x7f0800ef com.test.ui.TestFragment}
How can I add multiple instances of the same fragment?
One requirement is that they are all on top of each other - because few of them have a small margin that lets you see small portion of the fragment underneath.
Thank you for your help!