How can I implement FragmentManager and FragmentTransaction to replace just a single fragment?

android, fragment, fragmentmanager, fragmenttransaction

Solution

You use the FragmentManager like this:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method.
transaction.commit();

This code would replace the Fragment which sits in the View with the id R.id.bodyfragment with a new Instance of another Fragment.

EDIT:

To create a new instance of another Fragment a static factory method is supposed to be used. You would implement them in your Fragment like this:

public class AnotherFragment extends Fragment {

    public static AnotherFragment newInstance() {
        AnotherFragment fragment = new AnotherFragment();
        ...
        // do some initial setup if needed, for example Listener etc
        ...
        return fragment;
    }
    ...
}

Problem

I have an activity with 3 fragments; a header; a body; a footer (same point as in HTML). The bodyfragment contains three buttons which each should replace the middle fragment (body; itself) with another one, but I can't figure out how to work FragmentManager and FragmentTransition in here. I can't seem to find any coherency in other peoples questions on here with regards to the way others implement their fragments. It seems everyone has their own methods, or just doesn't include the full code in their threads. MainActivity.java ``` public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.test_frag); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } ``` } TestFragment.java ``` public class TestFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.test_frag, container, false); } ``` } BodyFragment.java ``` public class BodyFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.body_frag, container, false); } ``` } Fragment in XML ``` <fragment android:id="@+id/bodyfragment" android:name="com.example.test.BodyFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" tools:layout="@layout/body_frag" /> ``` BodyFragment layout in XML (button x3) ``` <Button android:id="@+id/bsettings" android:layout_width="130dp" android:layout_height="40dp" android:layout_alignBaseline="@+id/bgames" android:layout_alignBottom="@+id/bgames" android:layout_toRightOf="@+id/bgames" android:text="SETTINGS" /> ```

Original source