Android TabLayout does not reload fragments

I noticed that it TabLayout

only loads Fragments

which it has to display once, so when I replace the Fragment

containing TabLayout

one and return to it, it does not recreate Fragments

which one it is holding, and hence they do not display anything else. Is this design like this or am I doing something wrong? I am using external Fragment

in NavigationDrawer

, and when the user switches to another Fragment

and back, the tab is Fragments

empty because they are not being recreated. I also noticed that the tabs act very strangely after I go back to the snippet containing them (they are the white line below the hops, and you can hold it 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);
    }
}

      

+6


source to share


2 answers


I had the same problem and the answer on this page solved my problem. Just use getChildFragmentManager()

insteadgetSupportFragmentManager()



Android TabLayout no longer displays content as soon as fragment is switched

+14


source


Use for FragmentStatePagerAdapter instead of FragmentPagerAdapter

This will work.



thank

-1


source







All Articles