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" />
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Fragment fragment = new tasks(); FragmentManager fragmentManager = getActivity(). getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction(); fragmentTransaction. replace(R.
These two classes are clearly related. Is SupportFragmentManager used for Fragments generated using FragmentTransaction , while the "regular" FragmentManager is used exclusively to test Fragments generated using .
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;
}
...
}
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