I have a main Activity and one fragment.
The main Activity has the following layout:
<FrameLayout
...>
<LinearLayout
android:id="@+id/header"
.../>
<com.project.fragments.FirstFragment
.../>
</FrameLayout>
As the above layout shows, there is a <LinearLayout> with id “header“, and there is the FirstFragment which is part of the screen content. It works fine so far.
Then, I created another fragment class, called SecondFragment.java .
In my main activity java code, how can I dynamically switch the content between FirstFragment and SecondFragment meanwhile keep the <LinearLayout> (@+id/header) part stay on the screen?
(My app is running on a mobile phone not a tablet. Basically, I would like to have part of the screen content change by using fragments, but not sure how to achieve this.)
Here’s some template code for you to use: