Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" />
like image 898
Carl Avatar asked Dec 05 '13 13:12

Carl


People also ask

How do I replace one fragment with another?

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();

How do you move from one fragment to another in Kotlin?

setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Fragment fragment = new tasks(); FragmentManager fragmentManager = getActivity(). getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager. beginTransaction(); fragmentTransaction. replace(R.

What is the difference between FragmentManager and SupportFragmentManager?

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 .


1 Answers

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;
    }
    ...
}
like image 195
Xaver Kapeller Avatar answered Oct 02 '22 02:10

Xaver Kapeller