Android TabLayout not reloading fragments

2.6k views Asked by At

I noticed the TabLayout only loads the Fragments it has to display once, so when I replace the Fragment containing the TabLayout and switch back to it, it does not recreate the Fragments it holds, and thus they are no longer displaying anything. Is this by design like that or am I doing something wrong? I am using the outer Fragment in a NavigationDrawer, and when the user switches to another Fragment and back, the tab Fragments are empty because they don't get recreated. I also noticed the Tabs act really strange after I switch back to the fragment containing them (they white line below jumps and you can hold it still between the two tabs without touching the screen)

The code I'm using:

public class Fragment1 extends Fragment {
    private static ViewPager viewPager;
    private static TabLayout tabLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Utils.changeLanguage(getActivity());

        final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.LinesOverlay);

        LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

        View view = localInflater.inflate(R.layout.fragment1, container, false);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        tabLayout = (TabLayout) view.findViewById(R.id.tabs);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setupViewPager(viewPager);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        LinesTabsAdapter adapter = new LinesTabsAdapter(getActivity().getSupportFragmentManager());
        adapter.addFragment(new Fragment2(), "Fragment 1");
        adapter.addFragment(new Fragment3(), "Fragment 2");
        viewPager.setAdapter(adapter);
    }

    public static class Fragment2 extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.activity_lines_driving, container, false);

            return view;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    }

    public static class Fragment3 extends Fragment  {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.activity_lines_all, container, false);

            return view;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    }

LinesTabsAdapter:

public class LinesTabsAdapter extends FragmentPagerAdapter {
    private final List<Fragment> fragments = new ArrayList<>();
    private final List<String> fragmentTiles = new ArrayList<>();

    public LinesTabsAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        fragments.add(fragment);
        fragmentTiles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTiles.get(position);
    }
}
2

There are 2 answers

1
Yogesh Nikam Patil On

Use for FragmentStatePagerAdapter instead of FragmentPagerAdapter

It will work.

Thank you

2
arslanbenzer On

I had the same problem and answer in this page solved my problem. Just use getChildFragmentManager() instead of getSupportFragmentManager()

Android TabLayout does not display contents anymore as soon as fragment is switched