Content ViewPager is deleted when exiting the fragmen and returning with the button back

1

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

    
asked by OscarDaniel 21.03.2018 в 20:46
source

1 answer

0

You're doing something weird with your adapter. In fact, it's the first time I see a tabs adapter in this way. The immediate solution that I can give you is to implement it in another way, which I consider correct and is the one I always use.

public class TabsPagerAdapter extends FragmentStatePagerAdapter{

    public List<Fragment> mFragments;
    public List<String> titulos;


    public TabsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.mFragments = fragments;
    }

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

    @Override
    public int getCount() {
        return mFragments.size();
    }
@Override
public CharSequence getPageTitle(int position)
{
    return titulos.get(position);
}

Build the list of fragments in your activity and pass it already armed. It's a best practice AND use the FragmentStatePagerAdapter to help you with the fragments control on the screen. Good luck.

    
answered by 24.03.2018 в 05:02