The standard way to declare fragments in a xml layout file is
<LinearLayout ...>
<fragment class="com.example.SomeFragment"
</LinearLayout>
where SomeFragment is a java class defined like
class SomeFragment extends Fragment {
...
}
Lets say, I have 3 fragments; fragment1, fragment2, and fragment3. When the user launches the app, I show them fragment1, and when they click on a button, I replace the fragment1 with fragment2, etc.
What is the best approach to define the 3 fragments in a single layout xml file?
You should use a FrameLayout for that, that way you don’t have to specify the fragment class in the XML and that way it is not limited to one class.
and than you can set the fragment in the code like this