I have seen an approach where frameLayout is used in case of fragments. The ultimate goal was to have multiple fragments.
For showing a single Fragment immediately on the screen, yes, you can use fragment or FrameLayout interchangeably.
Showing the Fragment via the fragment tag would look like this in XML:
<fragment class="com.example.ExampleFragment"
android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
Showing the Fragment via FrameLayout would look like this in XML:
<FrameLayout android:id="@+id/details" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
Followed by Java code like this:
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.details, newFragment);
transaction.addToBackStack(null);
transaction.commit();
Method 2 then supports changing what fragment you are showing later by running more Java code to change what Fragment is there afterwards:
Fragment secondFragment = new SecondExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.details, secondFragment);
transaction.addToBackStack(null);
transaction.commit();
So FrameLayout gives you the extra ability to do that over using the fragment tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With