I am able to do a fragment transaction like this:
Fragment2 frag = new Fragment2();
FragmentTransaction tr = fragmentManager.beginTransaction();
tr.add(R.id.fragment, frag);
tr.addToBackStack();
tr.commit();
Simply in an activity which has this layout:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.youhou.fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment1" />
Where Fragment1
contains a normal LinearLayout
and Fragment2
too, like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
</LinearLayout>
I'm able to do a second transaction with Fragment3
again as follows:
Fragment3 frag = new Fragment3();
FragmentTransaction tr = fragmentManager.beginTransaction();
tr.add(R.id.fragment, frag);
tr.addToBackStack();
tr.commit();
And it seems to work but I know this should not be the case. Is it dangerous for the app stability to use it like this? Can someone provide me an explanation about this case, as why it work like this and how.
I know I should be using a Framelayout
and add/replace Fragment in it but in my case, I don't have time to change this.