I have a TabLayout on my Activity with three tabs, each attached to a fragment fragment. These all load and work properly. My problem is when I try and replace one of these fragments at runtime.
By targeting the ViewPager (R.id.view_pager
) object as the container, when I try and inflate the new fragment, both views disappear:
public void changeToNewFragment(){
NewFragment newFragment = new NewFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.view_pager, newFragment).addToBackStack(null).commit();
}
If I try and target the entire view, with R.id.container
, the new Fragment appears, overlaid on top of the current fragment:
public void changeToNewFragment(){
NewFragment newFragment = new NewFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, newFragment).addToBackStack(null).commit();
}
How do I inflate this new view, under the tabLayout, so that only this new view shows? This view should become part of the tabs, meaning if another tab is clicked, this view swipes away.