How to prevent tabs from being recreated on every activity call
I have an application with an index page that has tabs for navigation. The page has multiple subpages, which are implemented as fragments.
Some of my child fragment views also have tabs in them for more navigation like below
The problem I'm having is when I switch from one of the fragments to the other and back to it. Recreated the fragment's tab like this, and I'm not sure how to fix this as this is my first time this tab is bad.
Below is the code for my top level tag adapter
public class indexTabAdapter extends FragmentPagerAdapter
{
private List<String> titleList;
private List<Fragment> fragmentList;
public indexTabAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> titleList)
{
super(fm);
this.fragmentList = fragmentList;
this.titleList = titleList;
}
@Override
public Fragment getItem(int position)
{
return fragmentList.get(position);
}
@Override
public int getCount()
{
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position)
{
return titleList.get(position);
}
}
Below is the code for the activity class that manages the top level tags
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index_page);
pager = (ViewPager) findViewById(R.id.indexViewPager);
tabLayout = (TabLayout)findViewById(R.id.indexTabs);
prepareDataResource();
indexTabAdapter ita = new indexTabAdapter(getSupportFragmentManager(),fragmentList,titleList);
pager.setAdapter(ita);
tabLayout.setupWithViewPager(pager);
ita.notifyDataSetChanged();
}
private void prepareDataResource()
{
fragmentList.add(new dash());
titleList.add("Dash");
fragmentList.add(new AcademicCalender());
titleList.add("Calender");
fragmentList.add(new IncidentReport());
titleList.add("Incident Report");
fragmentList.add(new Profile());
titleList.add("Profile");
fragmentList.add(new Pta());
titleList.add("PTA");
fragmentList.add(new timeTable());
titleList.add("Time Table");
fragmentList.add(new Results());
titleList.add("Results");
}
Finally, here's the code for the low-level tags that keep repeating
public class Profile extends Fragment
{
private List<String> titleList = new ArrayList<>();
private List<Fragment> fragmentList = new ArrayList<>();
private ViewPager pager;
private TabLayout tabLayout;
public Profile()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.profile_view_activity,container,false);
pager = (ViewPager) view.findViewById(R.id.profileViewPager);
tabLayout = (TabLayout) view.findViewById(R.id.profileTabs);
prepareDataResource();
profileTabsAdapter rta = new profileTabsAdapter(getFragmentManager(),fragmentList,titleList);
pager.setAdapter(rta);
tabLayout.setupWithViewPager(pager);
return view;
}
private void prepareDataResource()
{
fragmentList.add(new index());
titleList.add("Home");
fragmentList.add(new immunization());
titleList.add("Immunization");
fragmentList.add(new information());
titleList.add("Information");
fragmentList.add(new records());
titleList.add("Health Records");
}
}
FragmentPagerAdapter
Instance your fragment once. So if you instantiate a list when it's declared as a field, it will always keep the list, adding elements when Fragment's is onCreateView
called .
To avoid this, declare and instantiate two lists as follows:
public class Profile extends Fragment {
private List<String> titleList;
private List<Fragment> fragmentList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//your code for views initialization
//init the lists here!
titleList = new ArrayList<>();
fragmentList = new ArrayList<>();
prepareDataResource();
//your adapter set up code
return view;
}