I have a fragment with tabs that makes use of a view pager. The issue is that the tabs appear when I first visit the fragment but if I navigate somewhere else and then go back to this fragment then the fragments within the view pager no longer appear.
I use a SectionsPagerAdapter as below:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position) {
case 0:
//ClientHealthRootFragment tab1 = new ClientHealthRootFragment();
ChatListFragment tab1 = new ChatListFragment();
return tab1;
case 1:
ClientProfileFragment tab2 = new ClientProfileFragment();
//ChatListFragment tab2 = new ChatListFragment();
return tab2;
case 2:
//ClientCommentRootFragment tab3 = new ClientCommentRootFragment();
ChatListFragment tab3 = new ChatListFragment();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Health";
case 1:
return "Profile";
case 2:
return "Comments";
}
return null;
}}
And then in the fragment where I handle the sections is the following:
public class ClientMainFragment extends Fragment {
private ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
String client_name;
int client_image;
private TextView clientNameText;
private ImageView clientImage;
public ClientMainFragment() {
//empty constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_client_main, container, false);
clientNameText = view.findViewById(R.id.clientNameTxt);
clientImage = view.findViewById(R.id.clientImage);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(mViewPager);
return view;
}}
I am unsure what I am missing, any help would be appreciated.
EDIT: Solution
I figured out what the issue was with the following link: Tab content disappeared after change page