How to prevent tabs from being recreated on every activity call


Raff

I have an application with an index page that has tabs for navigation. The page has multiple subpages, which are implemented as fragments.enter image description here

Some of my child fragment views also have tabs in them for more navigation like belowenter image description here

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.enter image description here

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");


    }
}
Romina virus

FragmentPagerAdapterInstance 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 onCreateViewcalled .

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;
    }

Related


How to prevent tabs from being recreated on every activity call

Raff 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

Prevent array from being recreated on every form submit

Acadian_Ghost I'm trying to store and display the session data from a form in 3 different arrays that are updated each time the form is submitted. The first time the form is submitted, the array data is displayed correctly, but after each time it seems to be r

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

How to prevent new Fragment from being recreated again and again?

stern bafsar Hello i made a simple program in android in which i used 3 Tabs in Tablayout. When I click on the first tab, it opens the first fragment, but when I go to the second tab, when I go back to the first tab, select "recreate" and overwrite my new frag

Prevent files from being recreated or overwritten

username The data is stored in the file as is, but when I restart the program, they disappear and the file size goes back to 0. Here's what I've come up with so far: if (!fp) { fp=fopen("data.txt", "wb"); } But it doesn't work. Thanks! Bill Lynch Let's

How to prevent activity from being initialized multiple times

Jason I have an android app where the same activity gets initialized multiple times when the user clicks a button multiple times quickly. To prevent this, I added the file android:launchMode="singleInstance"in the manifest file. But now, when an activity calls

How to prevent else statement from being fired every time

Starks I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the

How to prevent else statement from being fired every time

Starks I'm very new to coding, sorry for wasting your time on beginner questions. I'm trying to learn JS with an ebook and my exercise atm is to write a program to save the entered name (first and last name) and the entered gender. The program should check the