0

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.

Blo
  • 11,903
  • 5
  • 45
  • 99
An-droid
  • 6,433
  • 9
  • 48
  • 93
  • "but in this case I don't have time to change this. " - then whats the point of asking? You won't have time to do it, and it's not like it doesn't currently work... ? – Shark Jan 29 '16 at 13:05
  • I don't see any difference in both code blocks. – Rohit5k2 Jan 29 '16 at 13:06
  • Do you really need to know why i'm wanting to know why something works when i think it should not ? ;) and yes the transaction work like that, even if it seems badpractice – An-droid Jan 29 '16 at 13:08
  • @Rohit5k2 the question is about using a static layout fragment and doing a dynamic transaction on it. I don't understand how it works, normally the transaction need a container like a Layout to fill it – An-droid Jan 29 '16 at 13:11
  • I think this works because you specified this line: `layout="@layout/fragment1"` in activity's layout. So you added an inner `LinearLayout` in place of `fragment` tag element. Then your dynamic fragments might be added into the LinearLayout and fill in. It seems to me like a very bad practice, because you'll inevitably instantiate `Fragment1` before creating `Fragment2`, which can affect the memory managment. – Blo Jan 29 '16 at 13:40
  • it also feels like pushing a fragment's layout into the layout of another fragment and you know a fragment cannot contain another fragment... but i think i understand your reasonning – An-droid Jan 29 '16 at 13:59

0 Answers0