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

1

6

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" />
Accommodation answered 5/12, 2013 at 13:18 Comment(3)
There is a fairly straight-forward implementation example on the official documentation, why is that not working for you ? developer.android.com/guide/components/fragments.htmlRedundancy
@Redundancy That is the tutorial I am following, actually. The fragment examples in there differ from the way they teach you how to use FragmentManager and FragmentTransactions. I don't know if I should be calling FragmentManager from the Activity or the BodyFragment. I also don't know if I should be calling the transaction on just the bodyfragment or the activity itself. The documentation on it is very hard to read.Accommodation
You should put that code in the Activity, for example in onCreate. In transaction.replace(R.id.fragment_container, newFragment), fragment_container is the id of the container for the fragment in your Activity layout, newFragment is the Fragment you want to put in the container.Redundancy
G
11

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;
    }
    ...
}
Guthrun answered 5/12, 2013 at 13:23 Comment(2)
Do I place this within the BodyFragment.java's onCreateView? If I do, it wants me to create newInstance() method in AnotherFragment.java, which in return makes FragmentManager unreachable. Googling the result gives more discussion on the usage of newInstance() as opposed to any examples of usage.Accommodation
You place this for example in the onClickListener of a button, or wherever else you want to change the fragment. To create new Instances of Fragments static factory methods are supposed to be used and that's what newInstance() is in my example. I will edit my answer to clarify.Guthrun

© 2022 - 2024 — McMap. All rights reserved.