Reputation: 301
I have an app with a TabLayout / FragmentPagerAdapter to control four tabs. Three of the tabs work fine and display their individual fragments (no issues). The forth fragment, intermittently fails to show the contents of the view.
I have tested and the class for the view is loaded, but no contents. The Fragment does contain nested Fragments (they do not nest again), which I believe may be causing the issue. Below is the XML for the fragment with issues.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
</FrameLayout>
</LinearLayout>
Below is the class for the Fragment in question.
public class MyFragmentLayout extends Fragment {
View view;
SubFragment1 subFragment1 = new SubFragment1();
SubFragment2 subFragment2 = new SubFragment2();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.fragment_layout, container, false);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
return view;
}
public void showSubFragment2() {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment2);
fragmentTransaction.commit();
}
public void showSubFragment1() {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
}
}
Just to be clear. The issue I am having is after a period of time switching between the Tabs. This view, and this view alone, will no longer display. There seems to be no issue with the TabLayout as it is loading this class. I am hoping that someone might be able to provide some guidance in solving this issue.
Upvotes: 0
Views: 109
Reputation: 12526
You need to use getChildFragmentManager()
instead of getFragmentManager()
for placing and managing Fragments inside of a Fragment.
So
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = inflater.inflate(R.layout.fragment_layout, container, false);
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
return view;
}
public void showSubFragment2() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment2);
fragmentTransaction.commit();
}
public void showSubFragment1() {
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left);
fragmentTransaction.replace(R.id.fragmentContainer, subFragment1);
fragmentTransaction.commit();
}
Upvotes: 2
Reputation: 30985
Try calling getChildFragmentManager()
instead of getFragmentManager()
and see if that helps.
Upvotes: 0