So I have a Fragment(A) with a ListView. If I click the Listview's item, then I want to open another Fragment(B). My only problem is, that when I open the B Fragment, also the A is appear in the layout.
FragmentA nextFrag = new FragmentA(data);
getFragmentManager().beginTransaction()
.replace(R.id.fragment_a_layout, nextFrag)
.commit();
What is the proper way to open a new Fragment, without show the previuos one too?
@sweak So I understand, that I need an interface, which I already write in my FragmentA class:
public interface FragmentListener {
void replaceFragments();
}
Then I implement this interface in my MainActivity.class, if I am right.
@Override
public void replaceFragments() {
FragmentB nextFrag = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_layout, nextFrag)
.commit();
}
In my FragmantA onClickListener, I call this method: (FragmentListener listener)
listener.replaceFragments(recipeForFragment);
But now my problem is that my listener is null, so somehow I need to initializte, but I don't know how. Maybe I create a setter for it, in the FragmentA:
public void setFragmentListener(FragmentListener fragmentListener) {
this.listener = fragmentListener;
}
But where should I call this setter and when?