I have several fragments which have several tabs, the problem is that when changing the fragment and returning the button behind the navigation bar, the content does not appear, only the bar appears with the titles of the tabs.
This is my tabas adapter.
private List<Fragment> fragments;
private List<String> titulos;
public TabPagerAdapter(FragmentManager fm)
{
super(fm);
fragments = new ArrayList<>();
titulos = new ArrayList<>();
}
public void addFragment(Fragment fragment, String titulo)
{
fragments.add(fragment);
titulos.add(titulo);
}
@Override
public Fragment getItem(int position)
{
return fragments.get(position);
}
@Override
public int getCount()
{
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position)
{
return titulos.get(position);
}
}
and that's how I sent the data through the adapter.
appBar = parent.findViewById(R.id.appBar);
tabs = new TabLayout(getContext());
tabs.setTabTextColors(Color.parseColor("#FFFFFF"), Color.parseColor("#FFFFFF"));
appBar.addView(tabs);
viewPager = view.findViewById(R.id.viewPager);
adapter = new TabPagerAdapter(getActivity().getSupportFragmentManager());
adapter.addFragment(ArticuloFragment.getInstance("Teléfonos", getConsulta("Teléfonos"), usuarioId, permiso, idPVenta, puntoVenta), "Teléfonos");
adapter.addFragment(ArticuloFragment.getInstance("Chips", getConsulta("Chips"), usuarioId, permiso, idPVenta, puntoVenta), "Chips");
adapter.addFragment(ArticuloFragment.getInstance("Accesorios", getConsulta("Accesorios"), usuarioId, permiso, idPVenta, puntoVenta), "Accesorios");
viewPager.setAdapter(adapter);
tabs.setupWithViewPager(viewPager);
tabs.setTabGravity(TabLayout.GRAVITY_FILL);
Note: This error only susede with the fragments that contain tabs the others do not lose their content when returning with the back button of the navigation bar. I based on this example to do it link