8

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);
    }
}
qwertz
  • 6,206
  • 9
  • 40
  • 62
  • "I noticed the TabLayout only loads the fragments" -- [`TabLayout`](http://developer.android.com/reference/android/support/design/widget/TabLayout.html) has nothing to do with fragments. `ViewPager` has nothing to do with fragments. `LinesTabsAdapter` presumably is involved with fragments. – CommonsWare Jun 14 '15 at 19:57
  • The code in setupViewPager should load the fragment, right? – qwertz Jun 14 '15 at 20:36
  • I have no idea. AFAIK, you wrote `LinesTabsAdapter`, as there is no such class in the Android SDK. The only reason it appears to be involved with fragments is that your appear to be passing fragments into it. – CommonsWare Jun 14 '15 at 20:50
  • @CommonsWare I added the LinesTabAdapter in the question – qwertz Jun 15 '15 at 08:25
  • You'll have to call `notifyDataSetChanged()` at the end of `addFragment(...)` and anytime you modify the underlying set of fragments. – Eugen Pechanec Jun 15 '15 at 09:04
  • @EugenPechanec unfortunately, this did not fix the problem. The fragments still only get created once – qwertz Jun 15 '15 at 09:08
  • Check Below link its working for me : [enter link description here](https://stackoverflow.com/a/34436011/3392323) – SAndroidD Aug 16 '17 at 12:29

2 Answers2

21

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

Community
  • 1
  • 1
arslanbenzer
  • 489
  • 3
  • 16
1

Use for FragmentStatePagerAdapter instead of FragmentPagerAdapter

It will work.

Thank you

Yogesh Nikam Patil
  • 1,192
  • 13
  • 18